Skip to content

Commit 866a2e1

Browse files
picnixzAA-Turner
authored andcommitted
revert some changes in case __contains__ raises errors
1 parent d3466d7 commit 866a2e1

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

sphinx/util/typing.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,25 @@ def restify(cls: Any, mode: _RestifyMode = 'fully-qualified-except-typing') -> s
226226
# If the mode is 'smart', we always use '~'.
227227
# If the mode is 'fully-qualified-except-typing',
228228
# we use '~' only for the objects in the ``typing`` module.
229-
if mode == 'smart' or getattr(cls, '__module__', None) == 'typing':
230-
modprefix = '~'
231-
else:
232-
modprefix = ''
229+
#
230+
# With an if-else block, mypy infers 'mode' to be a 'str'
231+
# instead of a literal string (and we don't want to cast).
232+
module_prefix = '~' if mode == 'smart' or getattr(cls, '__module__', None) == 'typing' else ''
233233

234234
try:
235235
if ismockmodule(cls):
236-
return f':py:class:`{modprefix}{cls.__name__}`'
236+
return f':py:class:`{module_prefix}{cls.__name__}`'
237237
elif ismock(cls):
238-
return f':py:class:`{modprefix}{cls.__module__}.{cls.__name__}`'
238+
return f':py:class:`{module_prefix}{cls.__module__}.{cls.__name__}`'
239239
elif is_invalid_builtin_class(cls):
240240
# The above predicate never raises TypeError but should not be
241241
# evaluated before determining whether *cls* is a mocked object
242242
# or not; instead of two try-except blocks, we keep it here.
243-
return f':py:class:`{modprefix}{_INVALID_BUILTIN_CLASSES[cls]}`'
243+
return f':py:class:`{module_prefix}{_INVALID_BUILTIN_CLASSES[cls]}`'
244244
elif inspect.isNewType(cls):
245245
if sys.version_info[:2] >= (3, 10):
246246
# newtypes have correct module info since Python 3.10+
247-
return f':py:class:`{modprefix}{cls.__module__}.{cls.__name__}`'
247+
return f':py:class:`{module_prefix}{cls.__module__}.{cls.__name__}`'
248248
return f':py:class:`{cls.__name__}`'
249249
elif UnionType and isinstance(cls, UnionType):
250250
# Union types (PEP 585) retain their definition order when they
@@ -268,7 +268,7 @@ def restify(cls: Any, mode: _RestifyMode = 'fully-qualified-except-typing') -> s
268268
text = restify(cls.__origin__, mode)
269269
elif getattr(cls, '_name', None):
270270
cls_name = cls._name
271-
text = f':py:class:`{modprefix}{cls.__module__}.{cls_name}`'
271+
text = f':py:class:`{module_prefix}{cls.__module__}.{cls_name}`'
272272
else:
273273
text = restify(cls.__origin__, mode)
274274

@@ -302,12 +302,12 @@ def restify(cls: Any, mode: _RestifyMode = 'fully-qualified-except-typing') -> s
302302
# handle bpo-46998
303303
return f':py:obj:`~{cls.__module__}.{cls.__name__}`'
304304
elif hasattr(cls, '__qualname__'):
305-
return f':py:class:`{modprefix}{cls.__module__}.{cls.__qualname__}`'
305+
return f':py:class:`{module_prefix}{cls.__module__}.{cls.__qualname__}`'
306306
elif isinstance(cls, ForwardRef):
307307
return f':py:class:`{cls.__forward_arg__}`'
308308
else:
309309
# not a class (ex. TypeVar)
310-
return f':py:obj:`{modprefix}{cls.__module__}.{cls.__name__}`'
310+
return f':py:obj:`{module_prefix}{cls.__module__}.{cls.__name__}`'
311311
except (AttributeError, TypeError):
312312
return inspect.object_description(cls)
313313

0 commit comments

Comments
 (0)