Skip to content

Commit fa78d52

Browse files
author
Guido van Rossum
committed
Make get_type_hints() always return None. Fixes #183.
1 parent 6da9fc2 commit fa78d52

File tree

2 files changed

+10
-38
lines changed

2 files changed

+10
-38
lines changed

python2/test_typing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,14 @@ def test_all(self):
11871187
# Check that Text is defined.
11881188
assert 'Text' in a
11891189

1190+
def test_get_type_hints_dummy(self):
1191+
1192+
def foo(x):
1193+
# type: (int) -> int
1194+
return x + 1
1195+
1196+
assert typing.get_type_hints(foo) is None
1197+
11901198

11911199
if __name__ == '__main__':
11921200
main()

python2/typing.py

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,44 +1158,8 @@ def _get_defaults(func):
11581158

11591159

11601160
def get_type_hints(obj, globalns=None, localns=None):
1161-
"""Return type hints for a function or method object.
1162-
1163-
This is often the same as obj.__annotations__, but it handles
1164-
forward references encoded as string literals, and if necessary
1165-
adds Optional[t] if a default value equal to None is set.
1166-
1167-
BEWARE -- the behavior of globalns and localns is counterintuitive
1168-
(unless you are familiar with how eval() and exec() work). The
1169-
search order is locals first, then globals.
1170-
1171-
- If no dict arguments are passed, an attempt is made to use the
1172-
globals from obj, and these are also used as the locals. If the
1173-
object does not appear to have globals, an exception is raised.
1174-
1175-
- If one dict argument is passed, it is used for both globals and
1176-
locals.
1177-
1178-
- If two dict arguments are passed, they specify globals and
1179-
locals, respectively.
1180-
"""
1181-
if getattr(obj, '__no_type_check__', None):
1182-
return {}
1183-
if globalns is None:
1184-
globalns = getattr(obj, '__globals__', {})
1185-
if localns is None:
1186-
localns = globalns
1187-
elif localns is None:
1188-
localns = globalns
1189-
defaults = _get_defaults(obj)
1190-
hints = dict(obj.__annotations__)
1191-
for name, value in hints.items():
1192-
if isinstance(value, basestring):
1193-
value = _ForwardRef(value)
1194-
value = _eval_type(value, globalns, localns)
1195-
if name in defaults and defaults[name] is None:
1196-
value = Optional[value]
1197-
hints[name] = value
1198-
return hints
1161+
"""In Python 2 this is not supported and always returns None."""
1162+
return None
11991163

12001164

12011165
def no_type_check(arg):

0 commit comments

Comments
 (0)