Skip to content

Incorrect error for unions syntax in comments and strings (Python 3.9) #19722

@relikd

Description

@relikd

Bug Report

Minimal example python code is attached below. mypy output is:

main.py:4: error: X | Y syntax for unions requires Python 3.10  [syntax]
main.py:12: error: X | Y syntax for unions requires Python 3.10  [syntax]
Found 2 errors in 1 file (checked 1 source file)

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.9&gist=03da1f2a4b60eb95088e81958066deb5

just in case, here a copy of the gist

#!/usr/bin/env python3
class C3:
    futureDef: 'C2'
    two: 'str|None'


class C2:
    pass


class C1:
    ONE = None  # type: str|None

    def begin(self) -> None:
        C1.ONE = 'a'

Expected Behavior

This file should produce no errors

Actual Behavior

Both errors are incorrect, as both types are provided in a 3.8 safe way (encased in a string or as a comment)

Your Environment

  • Mypy version used: 1.17.1 (latest)
  • Python version used: 3.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-quoted-annotationsDetecting problems with quoted annotationstopic-runtime-semanticsmypy doesn't model runtime semantics correctly
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions