Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,30 @@ class D(C):
# verify that @no_type_check never affects bases
self.assertEqual(get_type_hints(C.meth), {'x': int})

def test_no_type_check_forward_ref_as_string(self):
class C:
foo: typing.ClassVar[int] = 7
class D:
foo: ClassVar[int] = 7
class E:
foo: 'typing.ClassVar[int]' = 7
class F:
foo: 'ClassVar[int]' = 7

expected_result = {'foo': typing.ClassVar[int]}
for clazz in [C, D, E, F]:
self.assertEqual(get_type_hints(clazz), expected_result)

def test_nested_classvar_fails_forward_ref_check(self):
class E:
foo: 'typing.ClassVar[typing.ClassVar[int]]' = 7
class F:
foo: ClassVar['ClassVar[int]'] = 7

for clazz in [E, F]:
with self.assertRaises(TypeError):
get_type_hints(clazz)

def test_meta_no_type_check(self):

@no_type_check_decorator
Expand Down
19 changes: 13 additions & 6 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
# legitimate imports of those modules.


def _type_check(arg, msg):
def _type_check(arg, msg, is_argument=False):
"""Check that the argument is a type, and return it (internal helper).

As a special case, accept None and return type(None) instead. Also wrap strings
Expand All @@ -118,12 +118,16 @@ def _type_check(arg, msg):

We append the repr() of the actual value (truncated to 100 chars).
"""
invalid_generic_forms = (Generic, _Protocol)
if not is_argument:
invalid_generic_forms = invalid_generic_forms + (ClassVar, )

if arg is None:
return type(None)
if isinstance(arg, str):
return ForwardRef(arg)
if (isinstance(arg, _GenericAlias) and
arg.__origin__ in (Generic, _Protocol, ClassVar)):
arg.__origin__ in invalid_generic_forms):
raise TypeError(f"{arg} is not valid as type argument")
if (isinstance(arg, _SpecialForm) and arg is not Any or
arg in (Generic, _Protocol)):
Expand Down Expand Up @@ -464,9 +468,10 @@ class ForwardRef(_Final, _root=True):
"""Internal wrapper to hold a forward reference."""

__slots__ = ('__forward_arg__', '__forward_code__',
'__forward_evaluated__', '__forward_value__')
'__forward_evaluated__', '__forward_value__',
'__forward_is_argument__')

def __init__(self, arg):
def __init__(self, arg, is_argument=False):
if not isinstance(arg, str):
raise TypeError(f"Forward reference must be a string -- got {arg!r}")
try:
Expand All @@ -477,6 +482,7 @@ def __init__(self, arg):
self.__forward_code__ = code
self.__forward_evaluated__ = False
self.__forward_value__ = None
self.__forward_is_argument__ = is_argument

def _evaluate(self, globalns, localns):
if not self.__forward_evaluated__ or localns is not globalns:
Expand All @@ -488,7 +494,8 @@ def _evaluate(self, globalns, localns):
localns = globalns
self.__forward_value__ = _type_check(
eval(self.__forward_code__, globalns, localns),
"Forward references must evaluate to types.")
"Forward references must evaluate to types.",
is_argument=self.__forward_is_argument__)
self.__forward_evaluated__ = True
return self.__forward_value__

Expand Down Expand Up @@ -998,7 +1005,7 @@ def get_type_hints(obj, globalns=None, localns=None):
if value is None:
value = type(None)
if isinstance(value, str):
value = ForwardRef(value)
value = ForwardRef(value, is_argument=True)
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.

Sorry for not having time to review this earlier, this all looks good except for the meaning is exactly the opposite to what I expected :-) There are two options:

  • Rename is_argument to is_not_argument everywhere (arguably ugly)
  • Carefully replace True with False and vice versa, and update the corresponding checks

@nnja @ambv Will any of you have time to make a PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ilevkivskyi cc/ @ambv Yes, I'll have time tomorrow evening on my flight home.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ivan, I don't think the meaning is reversed? As I understand it:

  • "argument" is what's directly an annotation on a function argument and so on; so it includes non-types
  • otherwise, it has to be a type (like inside generics)

If you still want to make a change to this, I would prefer renaming to is_annotation which signifies that we're talking about an entire annotations.

Copy link
Copy Markdown
Member

@ilevkivskyi ilevkivskyi May 17, 2018

Choose a reason for hiding this comment

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

Renaming to is_annotation is a possible option, but IMO is still less clear than is_argument with reversed value, since the latter makes simple distinction between these two cases:

  • List['int'], here 'int' appears as a type argument in another type
  • 'int' here 'int' appears just as a type

In the first case the rules are stricter, e.g. ClassVar can't appear as a type argument.

In view of this, the ideal option would be to both rename it to is_type_argument and reverse False to True, but I don't want to ask for too much :-) I will be happy with either option if there will be a short comment explaining what is_argument/is_annotation/is_type_argument means.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the term "type argument" used anywhere in typing? I only heard of them being called "generics".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

OK, cool, in this case you're right, let's just reverse True with False and vice versa.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ambv @ilevkivskyi Should I open a new PR for these changes? What's the needed course of action for the backport? #6912

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.

Yes, please make a PR against master branch. Also it makes sense to do this ASAP, since 3.7rc1 is out in one day.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ilevkivskyi 👍I can work on this tonight. Will you be available to review?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ilevkivskyi Done! The updated PR is #7039. Please let me know how it looks.

value = _eval_type(value, base_globals, localns)
hints[name] = value
return hints
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix failure in `typing.get_type_hints()` when ClassVar was provided as a string forward reference.