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

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

Closed
bzoracler opened this issue May 5, 2024 · 0 comments · Fixed by #17217
Closed

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

bzoracler opened this issue May 5, 2024 · 0 comments · Fixed by #17217
Labels
bug mypy got something wrong

Comments

@bzoracler
Copy link
Contributor

bzoracler commented May 5, 2024

Bug Report

Possibly related to #7482 and #5423

To Reproduce

Run mypyc on the following snippet:

import typing as t                             # Line 1

def process(o: int | str | None, /) -> object:  # Line 3
    if isinstance(o, int):
        return _process_int(o)
    elif isinstance(o, str):
        return _process_str(o)
    else:
        return o

def _process_str(o: str, /) -> str: return o
def _process_int(o: int, /) -> int: return o

Expected Behavior

Successful compilation

Actual Behavior

$ mypyc test.py
test.py:3: error: Local variable "o" has inferred type None; add an annotation

mypyc doesn't like the annotation int | str | None (if you add something unrelated to the union that doesn't exhaust the if...elif stack, e.g. bytes, the error goes away). What would be an alternative annotation which could be used here that reflects int | str | None, apart from object or typing.Any?

Your Environment

  • Mypy version used: 1.10
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.12
@bzoracler bzoracler added the bug mypy got something wrong label May 5, 2024
hauntsaninja pushed a commit that referenced this issue May 21, 2024
…17217)

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*:


https://github.com/python/mypy/blob/fb31409b392c5533b25173705d62ed385ee39cfb/mypy/nodes.py#L988

This causes an error at

https://github.com/python/mypy/blob/fb31409b392c5533b25173705d62ed385ee39cfb/mypyc/irbuild/expression.py#L161-L164

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant