Bug Report
Strangely, type-narrowing for unions including a Literal works for int but not datetime
To Reproduce
Mypy Playground. Copied here for convenience:
fromdatetimeimportdatetimefromtypingimportFinal, LiteralUnspecified=Literal['unspecified']
UNSPECIFIED: Final[Unspecified] ='unspecified'defdo_thing(x: datetime|Unspecified) ->None:
ifx!=UNSPECIFIED:
print(x.utcnow()) # Unexpected error!defdo_thing_2(x: int|Unspecified) ->None:
ifx!=UNSPECIFIED:
print(x.real) # No prob
Expected Behavior
No errors.
Actual Behavior
Has errors:
main.py:9: error: Item "str" of "datetime | Literal['unspecified']" has no attribute "utcnow" [union-attr]
Your Environment
- Mypy version used: 1.7.0
- Mypy command-line flags: <none>
- Mypy configuration options from
mypy.ini (and other config files): <none> - Python version used: 3.11
Bug Report
Strangely, type-narrowing for unions including a Literal works for
intbut notdatetimeTo Reproduce
Mypy Playground. Copied here for convenience:
Expected Behavior
No errors.
Actual Behavior
Has errors:
Your Environment
mypy.ini(and other config files): <none>