-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
MyPy disallows Tuple to be instantiated with variable number of items #8758
Comments
You can work around this by adding an explicit annotation of |
Tested your suggestion, but that does not work:
... results in
|
You should annotate only the first definition. |
Tried that, too - which brings back to the initial error:
Error:
|
You should annotate it (once) as |
Thank you, that worked. I had my own solution that involved Union, but I like this one better (I was not aware of the ... type annotation). But still - it is desired to have these mypy errors eliminated in the future releases. |
@vpogrebi This is not a mypy bug, the tuple infers the first type it encounters when assigned to, and assigning a different type (with different amount of items inside the tuple) later on is an error - this is valid behavior. You need to explicitly tell it to either accept a variable amount of items via __all__: Union[Tuple[str, str, str, str], Tuple[str, str, str]]
try:
...
except:
... |
I agree with @DevilXD that this doesn't really seem like a bug. |
Tuple in Python - is a fixed-length list ("frozen list"). But there's nothing in Python that prevents same-name Tuple to be instantiated with different number of items (which may well be based on some condition)...
I have an issue related to mypy, conditional import and validation of the __all package attribute (defined as a Tuple containing variable number of items depending on the success of the conditional import). Consider following within package's init.py:
When this code is checked by mypy, it raises following error:
This issue keeps me from building Python package in the presence of 'mypy' check; I had to explicitly disable mypy check in order to build this package.
I'd like to have mypy recognize possibility of variable-size Tuple in the presence of conditional import (and in general - same-name Tuple can be instantiated having different size depending on some conditions), or be able to ignore this error (unfortunately, mypy output does not report error number that can be ignored), or entirely exclude given file from mypy check (similarly to flake8's "exclude" in the setup.cfg).
The text was updated successfully, but these errors were encountered: