Skip to content
Prev Previous commit
Next Next commit
Fix yet another bug and add many more tests
  • Loading branch information
AlexWaygood committed Jun 9, 2024
commit 2fbcd7b10c5c614ae23eccfb2eadba67269e8ca2
47 changes: 44 additions & 3 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4858,7 +4858,7 @@ def f(x: X): ...
{'x': list[list[ForwardRef('X')]]}
)

def test_pep695_generic_with_future_annotations(self):
def test_pep695_generic_class_with_future_annotations(self):
original_globals = dict(ann_module695.__dict__)

hints_for_A = get_type_hints(ann_module695.A)
Expand All @@ -4867,15 +4867,21 @@ def test_pep695_generic_with_future_annotations(self):
self.assertEqual(hints_for_A["y"].__args__[0], Unpack[A_type_params[1]])
self.assertIs(hints_for_A["z"].__args__[0], A_type_params[2])

# should not have changed as a result of the get_type_hints() calls!
self.assertEqual(ann_module695.__dict__, original_globals)

def test_pep695_generic_class_with_future_annotations_and_local_shadowing(self):
hints_for_B = get_type_hints(ann_module695.B)
self.assertEqual(hints_for_B, {"x": int, "y": str, "z": bytes})

def test_pep695_generic_class_with_future_annotations_name_clash_with_global_vars(self):
hints_for_C = get_type_hints(ann_module695.C)
self.assertEqual(
set(hints_for_C.values()),
set(ann_module695.C.__type_params__)
)

def test_pep_695_generic_function_with_future_annotations(self):
hints_for_generic_function = get_type_hints(ann_module695.generic_function)
func_t_params = ann_module695.generic_function.__type_params__
self.assertEqual(
Expand All @@ -4886,6 +4892,13 @@ def test_pep695_generic_with_future_annotations(self):
self.assertIs(hints_for_generic_function["z"].__origin__, func_t_params[2])
self.assertIs(hints_for_generic_function["zz"].__origin__, func_t_params[2])

def test_pep_695_generic_function_with_future_annotations_name_clash_with_global_vars(self):
self.assertEqual(
set(get_type_hints(ann_module695.generic_function_2).values()),
set(ann_module695.generic_function_2.__type_params__)
)

def test_pep_695_generic_method_with_future_annotations(self):
hints_for_generic_method = get_type_hints(ann_module695.D.generic_method)
params = {
param.__name__: param
Expand All @@ -4896,8 +4909,36 @@ def test_pep695_generic_with_future_annotations(self):
{"x": params["Foo"], "y": params["Bar"], "return": types.NoneType}
)

# should not have changed as a result of the get_type_hints() calls!
self.assertEqual(ann_module695.__dict__, original_globals)
def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_vars(self):
self.assertEqual(
set(get_type_hints(ann_module695.D.generic_method_2).values()),
set(ann_module695.D.generic_method_2.__type_params__)
)

def test_pep_695_generics_with_future_annotations_nested_in_function(self):
results = ann_module695.nested()

self.assertEqual(
set(results.hints_for_E.values()),
set(results.E.__type_params__)
)
self.assertEqual(
set(results.hints_for_E_meth.values()),
set(results.E.generic_method.__type_params__)
)
self.assertNotEqual(
set(results.hints_for_E_meth.values()),
set(results.E.__type_params__)
)
self.assertEqual(
set(results.hints_for_E_meth.values()).intersection(results.E.__type_params__),
set()
)

self.assertEqual(
set(results.hints_for_generic_func.values()),
set(results.generic_func.__type_params__)
)

def test_extended_generic_rules_subclassing(self):
class T1(Tuple[T, KT]): ...
Expand Down
32 changes: 32 additions & 0 deletions Lib/test/typinganndata/ann_module695.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,42 @@ def generic_function[T, *Ts, **P](
) -> None: ...


def generic_function_2[Eggs, **Spam](x: Eggs, y: Spam): pass


class D:
Foo = int
Bar = str

def generic_method[Foo, **Bar](
self, x: Foo, y: Bar
) -> None: ...

def generic_method_2[Eggs, **Spam](self, x: Eggs, y: Spam): pass


def nested():
from types import SimpleNamespace
from typing import get_type_hints

Eggs = bytes
Spam = memoryview


class E[Eggs, **Spam]:
x: Eggs
y: Spam

def generic_method[Eggs, **Spam](self, x: Eggs, y: Spam): pass


def generic_function[Eggs, **Spam](x: Eggs, y: Spam): pass


return SimpleNamespace(
E=E,
hints_for_E=get_type_hints(E),
hints_for_E_meth=get_type_hints(E.generic_method),
generic_func=generic_function,
hints_for_generic_func=get_type_hints(generic_function)
)
15 changes: 6 additions & 9 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,15 +1069,12 @@ def _evaluate(self, globalns, localns, type_params=_sentinel, *, recursive_guard
# but should in turn be overridden by names in the class scope
# (which here are called `globalns`!)
if type_params:
if self.__forward_is_class__:
globalns, localns = dict(globalns), dict(localns)
for param in type_params:
param_name = param.__name__
if param_name not in globalns:
globalns[param_name] = param
localns.pop(param_name, None)
else:
localns = {param.__name__: param for param in type_params} | localns
globalns, localns = dict(globalns), dict(localns)
for param in type_params:
param_name = param.__name__
if not self.__forward_is_class__ or param_name not in globalns:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the __forward_is_class__ condition? I don't understand why class-scoped forwardrefs are special here.

Copy link
Copy Markdown
Member Author

@AlexWaygood AlexWaygood Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For functions, names in type_params should always take highest priority, and this is implemented by inserting the type params into the local namespace (which is here called globalns) and popping them from the global namespace (which is here called localns). For classes, we only want to give type_params highest priority if they haven't been overridden in the local scope, however.

globalns[param_name] = param
localns.pop(param_name, None)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to do this? Shouldn't the local namespace override the type params?

Test case:

class X[T]:
    T = int
    x: T

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already have that test case included in this PR, and it fails unless we have this block of code here. The local namespace shoudl indeed override the type param, but the locals here are found in the globalns, and the globals here are found in the localns variable. This highly confusing situation is because the globals and locals are swapped higher up in the call stack here, for backwards compatibility reasons if you believe the comment:

cpython/Lib/typing.py

Lines 2419 to 2426 in a3711af

if localns is None and globalns is None:
# This is surprising, but required. Before Python 3.10,
# get_type_hints only evaluated the globalns of
# a class. To maintain backwards compatibility, we reverse
# the globalns and localns order so that eval() looks into
# *base_globals* first rather than *base_locals*.
# This only affects ForwardRefs.
base_globals, base_locals = base_locals, base_globals

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that comment, maybe we should find a way to get rid of that compatibility hack.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see a way round it, short of deprecating get_type_hints altogether. Which... I'm not 100% against, but it would be quite disruptive.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There could be other approaches, like adding new keyword arguments. Will have to think about it more.

Copy link
Copy Markdown
Member

@picnixz picnixz Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you decide to add keywords arguments or deprecate things, please tell me in advance so that I can be reactive on Sphinx' side if needed.


type_ = _type_check(
eval(self.__forward_code__, globalns, localns),
Expand Down