-
Notifications
You must be signed in to change notification settings - Fork 768
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
Pylance only cares about the last assignment of type definitions #4260
Comments
This is working as designed. When you provide a type annotation for a symbol, you are declaring that any value assigned to that symbol must conform to the provided type. It therefore doesn't make sense to declare two or more conflicting types for a symbol. This is considered a coding error. In your code, you have multiple incompatible type declarations for a symbol. To better understand static typing concepts including type declarations, refer to this documentation. |
As I mentioned in the other issue, if you want to allow a symbol to accept values of different types, then you can omit the type declaration altogether. The type will then be inferred based on the most recent assignment. If you include a type declaration, the type checker will enforce that any assigned values conform to that type. And if you provide multiple incompatible type declarations, the type checker will report that as an error. |
In this case, can we report error when user reassigns a type annotation? I think it helps locating the error. As @erictraut said,
hence, the coding error should be reported on the line where it occurs. |
Pylance will report the redefinition of a type alias if you use the If you're interested in receiving errors like this for type alias definitions, you should use the explicit U: TypeAlias = Literal["1"] |
😆After seeing your response in #4262, I just tested and it did report the correct error at the correct line. like this: P.S.I just found that erictr is the author of PEP 695. |
Pylance only recognizes the last assignment of a type definition and reports an error on code that uses a different type value from previous assignments. This can occur with both simple types and complex types like Literal and Tuple.
Environment data
Code Snippet
Repro Steps
Expected behavior
I offer two options but there might be better ones:
Actual behavior
Pyright only uses the last type definition
Logs
The 1858 line log is too long. I replaced my root dir path with
~
, and removed indexing fromnumpy
,sympy
,networkx
,pathspec
.After a lot of effort, github still says "There was an error creating your Issue: body is too long (maximum is 65536 characters)." So I share the log with an attachment
log.txt
The text was updated successfully, but these errors were encountered: