Skip to content

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

Closed
@bzoracler

Description

@bzoracler

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions