Skip to content
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

fix: annotated argument's var node type is explicit, not inferred #17217

Merged
merged 1 commit into from May 21, 2024

Conversation

bzoracler
Copy link
Contributor

Fixes #17216

During conversion from a standard library AST to the mypy AST, Var nodes were being created inside Argument nodes without acknowledging the presence of a type annotation, leading to the Var node's type as being always set as inferred:

self.is_inferred = self.type is None

This causes an error at

and expr.node.is_inferred
):
builder.error(
'Local variable "{}" has inferred type None; add an annotation'.format(

The fix simply acknowledges any presence of a type annotation, so the type of the relevant Var node is no longer considered inferred if an annotation is present.

Copy link
Contributor

github-actions bot commented May 6, 2024

Diff from mypy_primer, showing the effect of this PR on open source code:

spark (https://github.com/apache/spark)
+ python/pyspark/core/rdd.py:1662: error: Function does not return a value (it only ever returns None)  [func-returns-value]
+ python/pyspark/core/rdd.py:1664: error: Unused "type: ignore" comment  [unused-ignore]

@bzoracler
Copy link
Contributor Author

bzoracler commented May 6, 2024

Primer outputs look OK:

  • First warning is correct
  • Second warning (overloads) stems from an unrelated bug:
    from collections.abc import Callable
    
    f: Callable[[object], None] = lambda _: None
    
    a = f("a")  # Correct error: Function does not return a value (it only ever returns None) [func-returns-value]
    reveal_type(a)  # Bug: revealed type is `Any` (should be `None`)

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@hauntsaninja hauntsaninja merged commit ca393dd into python:master May 21, 2024
18 checks passed
@bzoracler bzoracler deleted the fix/var-type-inferred branch May 22, 2024 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[mypyc] ... inferred type None; add an annotation on fully-annotated code
2 participants