◐ Shell
clean mode source ↗

Message 415627 - Python tracker

As a consumer of `get_type_hints()` I think it'd be valuable to even have partially resolved types. My use case is that I provide an `Annotated` alias with a marker, and all I care about when inspecting user type hints is whether or not the arguments of an `Annotated` type contains my marker object. So ideally the fallback to an unresolved string or a sentinel object such as the proposed `typing.Unresolvable` should happen at the "lowest resolvable level" so that what can be resolved isn't lost.


By example, I'm saying that I think that this code:

    marker = object()

    def dec(cls):
        print(get_type_hints(cls))
        return cls

    @dec
    class A(abc.ABC):
        forward: Annotated[B, marker]

    class B:
        ...

Should produce:

    {"forward": Annotated[Unresolvable["B"], marker]}

I guess this would apply in situations where for instance a part of a union isn't resolvable too. If we have a union A|B where A is resolvable and B isn't, it should be resolved to:

    A | Unresolvable["B"]

And not to:

    Unresolvable["A | B"]

(I think for this perspective it's irrelevant whether unresolved types have a sentinel type or are just represented as strings).

(Here's the library that's my use case for the curious: https://github.com/antonagestam/abcattrs)