Type aliases to PEP 604 unions using class from untyped package "not valid as a type"
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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 https://github.com/python/mypy/issues/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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the reported case in test.py with mypy 1.6.1 and Python 3.10.10, comparing the failing alias str | BaseGeometry with the working str | Any case. Trace the type-alias validation path for classes imported from untyped packages; done means the reproduction reports no valid-type error while preserving the existing workaround behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100