-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
The base class is erroneously inferred for TypeVar constrained types #16051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think mypy is correct in generating an error with the constrained TypeVar. You've indicated that the TypeVar The other two cases (where |
@erictraut Thanks for the insights. Well, In practical terms, it's really this one that matters (even though it's not as good a reproduction case because it introduces explicit Q = TypeVar("Q", bound=B) , because listing all possible types is counterproductive. For example, consider that The docs for Also, given that Pyright handles |
The options 2 and 3 work now. The option 1 (type variable with constraints) doesn't work, but we have #1533 to track that kind of union math. |
@ilevkivskyi Thank you for fixing these two cases. It does make sense to track the constraint/union case as a separate issue. |
Bug Report
When a generic type defined via
TypeVar
is used with constrained types, a base class for those types is computed and errors are reported against that base class.To Reproduce
In this example,
get()
uses some internal logic to select a constructor, which is eitherD1
orD2
, andadd()
constructs the specified instance, performs some work with it and returns it to the caller.Expected Behavior
No errors should be reported because all referenced types align against one another and the base class
B
plays no role in this code. Moreover, ifB
is removed as a base class ofD1
andD2
, the errors are still reported, but againstobject
instead ofB
.I expect
TypeVar
with this constraint to behave as ifadd()
is defined as below, which reports no errors.Actual Behavior
Your Environment
mypy.ini
(and other config files): N/A (none)The text was updated successfully, but these errors were encountered: