-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
"Tuple expression not allowed in type annotation" error when pyright type checking is disabled #5451
Comments
Could someone from the pylance team please transfer this to pyright, since it's a core type checking issue? Thanks! |
I don't consider this a bug, but I'll treat it as a feature request. A tuple expression is not valid within a type annotation, so pyright is correct to generate an error here. This isn't a type violation; it's akin to a syntax error, since this syntax is not valid for annotations. For that reason, it's not currently disabled when type checking is disabled. I presume that you're using pylance for its language server features. These features leverage type annotations if present, so if you're using invalid type annotation forms, the language server features in pylance will not work in those cases. If you want your code to work with other tools, you should stick with valid annotation forms. I think it's unfortunate that basedmypy has chosen to support non-standard annotation expression forms. This will create friction with other tools that are standards-compliant. I encourage you to work with the typing community to standardize features that you want to see in the Python type system rather than creating non-standard variants. It was relatively easy to move this diagnostic to the |
that's fair. i'm not asking for pylance to support non-standard type annotations. all i want is for it to not show errors on them when i've explicitly disabled pylance's type checking |
Basedmypy intentionally has non-standard and breaking changes, it's intended to be as based as possible to promote beneficial changes within the typing ecosystem. |
To me the def validate_type(
x: tuple[int, str] | tuple[int, bytes] | tuple[int, bytearray]
) -> bool:
# check if x is a tuple of length 2 and the first element is an int
# and the second element is a str, bytes or bytearray
accpeted_types = {(int, str), (int, bytes), (int, bytearray)}
for t in accpeted_types:
for x_ele, t_ele in zip(x, t, strict=True):
if not isinstance(x_ele, t_ele):
break
else:
return True
return False
# test for validate_type
assert validate_type((1, "a")) == True
assert validate_type((1, b"a")) == True
assert validate_type((1, bytearray(b"a"))) == True
assert validate_type((1, 1)) == False
assert validate_type((1, 1.0)) == False a worse exampledef convert(x: int | str) -> str | int:
# Convert x to a string if it is an integer, or to an integer if it is a string.
valid_types = (int, str)
if type(x) not in valid_types:
raise TypeError(f"Expected type {valid_types}, got {type(x)}")
if type(x) == int:
return str(x)
else:
return int(x) # raise ValueError if x is not a valid integer
# test of convert
assert convert(1) == "1"
assert convert("1") == 1 |
This is fine in areas where you don't violate the typing standard or the conventions that have been agreed upon by all major type checkers. In cases where you diverge from the standard, you are creating unnecessary friction with other tools. Please work within the typing community to standardize such changes or stick with the standards if you can't achieve consensus. This may require a bit more work on your part than sitting on the side and maintaining your own (incompatible) fork of mypy, but it's the right thing to do for the community. |
thanks :) btw i believe there are many more errors that could be moved as well (i assume microsoft/pylance-release#2982 was the same issue). imo only errors that prevent any further analysis of the file (eg. syntax errors) should be displayed when type checking is disabled. i'll raise separate issues as i encounter them not only for the purpose of using non-standard type checkers, there are other situations where i've had code that normal mypy was happy with but pyright wasn't |
This comment was marked as off-topic.
This comment was marked as off-topic.
weren't type hints intentionally designed to allow them to be used in non-standard ways? https://peps.python.org/pep-0484/#abstract:
a specific example is the from typing import no_type_check
@no_type_check
def foo(a: (int, str)) -> None: # still errors
... |
@DetachHead PEP-484 was kinda old (29-Sep-2014) and it says "this PEP(484) still explicitly does NOT prevent other ..." .
|
PEPs are point-in-time design documents. They are not living documentation, and they are not typically updated when they are superseded or aspects are clarified by agreement among the major type checkers. PEP 484 was intentionally vague on many aspects of the typing system, and many of those value parts have been clarified in the many years since PEP 484 was written. The use of tuple literals in type annotations is one such example. There is a well-defined and standardized way to specify tuple types using |
its hard to know what the current consensus is on stuff like this when none of the existing documentation has been updated. if the PEPs don't get updated fair enough (though it would be nice if there was a message at the top mentioning that parts of it were superseeded in later PEPs), but i would at least expect a deprecation message on |
This is included in pyright 1.1.317, which I just published. It will also be included in future releases of pylance. |
i'm using an alternate type checker (basedmypy) that supports additional features such as tuple literal types, which is why i have pylance type checking disabled. however it seems that some errors such as this one are still displayed regardless of that setting
Environment data
Code Snippet
Repro Steps
set
"python.analysis.typeCheckingMode"
to"off"
insettings.json
Expected behavior
no error
Actual behavior
The text was updated successfully, but these errors were encountered: