Skip to content

Type aliases to PEP 604 unions using class from untyped package "not valid as a type" #16325

@robertschweizer

Description

@robertschweizer

Bug Report

When I create a type alias to PEP604-style union type, using a type from a package without type annotations, mypy complains that it's "not valid as a type".

Similar to #11280, but using a class from an untyped package.

Interestingly, things work if I explicitly use str | Any.

To Reproduce

from shapely.geometry import BaseGeometry

b = str | BaseGeometry 
my_var: b

Expected Behavior

No error. Mypy should treat BaseGeometry like Any.

Actual Behavior

test.py:4: error: Variable "test.b" is not valid as a type  [valid-type]
test.py:4: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.6.1
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.10.10

Workaround

Explicitly annotate variable as TypeAlias.

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