typing_extensions icon indicating copy to clipboard operation
typing_extensions copied to clipboard

Add missing dunder attributes for TypeAliasType instances

Open Daraan opened this issue 1 year ago • 1 comments

Addresses

  • https://github.com/python/typing_extensions/issues/469

As @JelleZijlstra mentioned that GenericAlias can be used to address #469. GenericAlias seems promising, however I am not sure what greater underlying changes this swap could maybe cause.

Currently I am still tracing some errors and I want to make sure I am not regressing some cases that currently are not covered by tests. This is also a reason this is currently only used for 3.10+. See below I think using it in 3.9 will cause more harm than benefits.

Daraan avatar Sep 25 '24 15:09 Daraan

From what I see GenericAlias can only be safely used here for Python3.10+

In 3.9 it will raise an TypeError in belows test as it tries to subscript the _ConcatenateGenericAlias which inherits from list. The location where this is causes is located here https://github.com/python/cpython/blob/8b9a8e0e082f849f6db03b2ed9074a86813b3b77/Lib/typing.py#L779

With _GenericAlias I can get the below test to pass in #449 however with GenericAlias this seems tricky.

    def test_callable_with_concatenate(self):
        P = ParamSpec('P')
        CallableP = TypeAliasType("CallableP", Callable[P, Any], type_params=(P,))

        callable_concat = CallableP[Concatenate[int, P]]
        self.assertEqual(callable_concat.__parameters__, (P,))
        concat_usage = callable_concat[str]  # <-- will fail for 3.9 with GenericAlias

Daraan avatar Sep 25 '24 17:09 Daraan