Closed Daraan closed 2 months ago
Currently the 3.13.0 typing.TypeAliasType
variant passes the following tests:
def test_subscription_without_type_params(self):
Simple = TypeAliasType("Simple", int)
with self.assertRaises(TypeError, msg="Only generic type aliases are subscriptable"):
Simple[int]
with self.assertRaises(TypeError, msg="Only generic type aliases are subscriptable"):
Simple[[]]
with self.assertRaises(TypeError, msg="Only generic type aliases are subscriptable"):
Simple[()]
# A TypeVar in the value does not allow subscription
T = TypeVar('T')
MissingTypeParamsErr = TypeAliasType("MissingTypeParamsErr", List[T])
self.assertEqual(MissingTypeParamsErr.__type_params__, ())
self.assertEqual(MissingTypeParamsErr.__parameters__, ())
with self.assertRaises(TypeError, msg="Only generic type aliases are subscriptable"):
MissingTypeParamsErr[int]
with self.assertRaises(TypeError, msg="Only generic type aliases are subscriptable"):
MissingTypeParamsErr[[]]
with self.assertRaises(TypeError, msg="Only generic type aliases are subscriptable"):
MissingTypeParamsErr[()]
# However, providing type_params=() argument allows subscription
MissingTypeParams = TypeAliasType("MissingTypeParams", List[T], type_params=())
self.assertEqual(MissingTypeParams.__type_params__, ())
self.assertEqual(MissingTypeParams.__parameters__, ())
# These do not raise
MissingTypeParams[int]
MissingTypeParams[[]]
MissingTypeParams[()]
# These do not raise
Simple2 = TypeAliasType("Simple2", int, type_params=())
Simple2[int]
Simple2[[]]
Simple2[()]
EDIT: This behavior has now been changed and errors are raised like expected. #473 will align the behavior with the latest cpython implementation.
When using a
TypeAliasType
without type_params it should raise an error which it currently doesn't:However, currently in 3.12+ when using
type_params=()
the code unexpectedly allows subscription. See also cpython issue #124498 and its PR. This issue likely needs an upstream decision first if the following case is intended or a bug:Also related:
239