Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behaviour of Optional in regards to Type[T] #9410

Closed
septatrix opened this issue Sep 3, 2020 · 5 comments
Closed

Inconsistent behaviour of Optional in regards to Type[T] #9410

septatrix opened this issue Sep 3, 2020 · 5 comments
Labels

Comments

@septatrix
Copy link
Contributor

🐛 Bug Report

Optional[str] is valid as an argument to cast, however invalid as a value of a variable of type Type.

To Reproduce

from typing import *

reveal_type(cast)

a = cast(7, True)

b = cast(Optional[int], a)
reveal_type(b)

c: Dict[str, Type]
c = {'foo': Optional[str]}
reveal_type(c)

d = {'bar': Optional[str]}
reveal_type(d)
$ mypy opt.py	
opt.py:3: note: Revealed type is 'Overload(def [_T] (typ: Type[_T`-1], val: Any) -> _T`-1, def (typ: builtins.str, val: Any) -> Any)'
opt.py:5: error: Invalid type: try using Literal[7] instead?
opt.py:8: note: Revealed type is 'Union[builtins.int, None]'
opt.py:11: error: Dict entry 0 has incompatible type "str": "object"; expected "str": "Type[Any]"
opt.py:12: note: Revealed type is 'builtins.dict[builtins.str, Type[Any]]'
opt.py:15: note: Revealed type is 'builtins.dict[builtins.str*, builtins.object*]'
Found 2 errors in 1 file (checked 1 source file)

Expected Behavior

The assignment to c passes type checking.

Actual Behavior

Optional[str] is apparently recognized as builtins.object and the assignment to c get recognized as an invalid assignment. However the call to cast (which itself does get typechecked) passes validation but also expects a Type.

Your Environment

  • Mypy version used: mypy 0.782
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): nothing
  • Python version used: Python 3.8.2
  • Operating system and version: Ubuntu 20.04.1 LTS
@septatrix septatrix added the bug mypy got something wrong label Sep 3, 2020
@gvanrossum
Copy link
Member

Quick answer: Type expects a class, not other forms of type -- in particular, Union is not allowed.

If you have more questions around this, please ask on Gitter.

@gvanrossum gvanrossum added question and removed bug mypy got something wrong labels Sep 3, 2020
@septatrix
Copy link
Contributor Author

Yes but if this is the case I do not understand why using Optional[int] as the first argument to cast does not generate an error.

@septatrix
Copy link
Contributor Author

But why does this work for cast then? Is this an error in the typeshed?

@gvanrossum
Copy link
Member

Please ask on Gitter.

@septatrix
Copy link
Contributor Author

For anyone else stumbling across this:
The issue was due to python/typeshed#4810 and what I was trying to is not yet possible AFAIK until we have something like #9773.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants