Skip to content

Commit 9e4ddf1

Browse files
committed
make sure that fix for python/typing#574 still works
1 parent 0fe1129 commit 9e4ddf1

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

sphinx_autodoc_typehints.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,21 @@ def _future_annotations_imported(obj):
269269
def get_all_type_hints(obj, name):
270270
rv = {}
271271

272-
# If one is using PEP563 annotations, Python will raise a TypeError on
273-
# e.g. 'str | None', therefore we accept TypeErrors if 'annotations'
274-
# is imported from '__future__'.
275-
ignore_errors = (AttributeError, RecursionError)
276-
catch_errors = (NameError,)
277-
if _future_annotations_imported(obj):
278-
catch_errors += (TypeError,)
279-
else:
280-
ignore_errors += (TypeError,)
281-
282272
try:
283273
rv = get_type_hints(obj)
284-
except ignore_errors:
274+
except (AttributeError, TypeError, RecursionError) as exc:
285275
# Introspecting a slot wrapper will raise TypeError, and and some recursive type
286276
# definitions will cause a RecursionError (https://github.com/python/typing/issues/574).
287-
pass
288-
except catch_errors as exc:
277+
278+
# If one is using PEP563 annotations, Python will raise a (e.g.,)
279+
# TypeError("TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'")
280+
# on 'str | None', therefore we accept TypeErrors with that error message
281+
# if 'annotations' is imported from '__future__'.
282+
if (isinstance(exc, TypeError)
283+
and _future_annotations_imported(obj)
284+
and "unsupported operand type" in exc.args[0]):
285+
rv = obj.__annotations__
286+
except NameError as exc:
289287
logger.warning('Cannot resolve forward reference in type annotations of "%s": %s',
290288
name, exc)
291289
rv = obj.__annotations__

0 commit comments

Comments
 (0)