You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Handle type[T] calls where T is bounded by a class (#2381)
Summary:
When types of `type[T]` are called, we currently infer a callable that takes any arguments and returns the type variable T. This is a conformance gap, where we should treat such calls as on `T`. However, the spec made clear that this is not free of problems:
From https://typing.python.org/en/latest/spec/constructors.html#constructor-calls-for-type-t
> It should be noted that such code could be unsafe because the type `type[T]` may represent subclasses of `T`, and those subclasses could redefine the `__new__` and `__init__` methods in a way that is incompatible with the base class. Likewise, the metaclass of `T` could redefine the `__call__` method in a way that is incompatible with the base metaclass.
If `T` is unrestricted, we assume an implicit bound of `object`, and use the callable type `() -> T`.
If `T` is bounded, we use the bound class type to infer the callable signature, but use the type `T` as the return type.
We cannot handle constrained type variables, since we don't have any intersection type support at the call target level, so I'm leaving the current behaviour as is.
Let's assess the impact on open source codebases, since I expect this change will bring some false positives.
#2369
Pull Request resolved: #2381
Test Plan: Ran conformance test suite and fixed 4 failing tests.
Reviewed By: stroxler
Differential Revision: D92900019
Pulled By: migeed-z
fbshipit-source-id: a50b259d559a938e75801f0fd44aff33e83051d7
0 commit comments