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

Accept more generic types as type[C] #15662

Closed
eltoder opened this issue Jul 13, 2023 · 2 comments
Closed

Accept more generic types as type[C] #15662

eltoder opened this issue Jul 13, 2023 · 2 comments
Labels
feature topic-type-form TypeForm might fix this

Comments

@eltoder
Copy link

eltoder commented Jul 13, 2023

Feature

It would be nice if the following worked:

from typing import Annotated, List, TypeVar

T = TypeVar("T")

def convert(val: object, ty: type[T]) -> T:
    raise NotImplementedError

reveal_type(convert([1], list[int]))
reveal_type(convert([1], List[int]))
reveal_type(convert(None, int | None))
reveal_type(convert(1, Annotated[int]))

Currently the first 2 calls type check as expected, but the following 2 calls fail with

type_t.py:10: note: Revealed type is "<nothing>"
type_t.py:10: error: Argument 2 to "convert" has incompatible type "UnionType"; expected "type[<nothing>]"  [arg-type]
type_t.py:11: note: Revealed type is "<nothing>"
type_t.py:11: error: Argument 2 to "convert" has incompatible type "object"; expected "type[<nothing>]"  [arg-type]

Technically int | None is not a type object, but neither are List[int] and list[int]. However, they are type specifications recognized by type checkers, and it is clear what the meaning of the call is.

Another option is to provide a new type like TypeSpec[T] to use for cases where the value is not necessarily a type object but is a valid type specification, if extending type is deemed not appropriate. The difference may be too subtle, though, and type already accepts some values which are not types per the example above.

If this works, type checkers can remove special cases for typing.cast function. It can be annotated simply as

def cast(typ: type[_T], val: Any) -> _T: ...
@AlexWaygood AlexWaygood added the topic-type-form TypeForm might fix this label Jul 13, 2023
@AlexWaygood
Copy link
Member

Closing as a duplicate of #9003. See also #9773 for discussion on the broader theme here.

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Jul 13, 2023
@eltoder
Copy link
Author

eltoder commented Jul 13, 2023

Ah yeah, it's basically a duplicate of #9773. I failed to find it. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-type-form TypeForm might fix this
Projects
None yet
Development

No branches or pull requests

2 participants