mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Add sphinx.util.inspect:isgenericalias()
This commit is contained in:
parent
9988d5ce26
commit
e20f29e325
@ -13,6 +13,7 @@ import enum
|
||||
import inspect
|
||||
import re
|
||||
import sys
|
||||
import types
|
||||
import typing
|
||||
import warnings
|
||||
from functools import partial, partialmethod
|
||||
@ -304,6 +305,18 @@ def isproperty(obj: Any) -> bool:
|
||||
return isinstance(obj, property)
|
||||
|
||||
|
||||
def isgenericalias(obj: Any) -> bool:
|
||||
"""Check if the object is GenericAlias."""
|
||||
if (hasattr(typing, '_GenericAlias') and # only for py37+
|
||||
isinstance(obj, typing._GenericAlias)): # type: ignore
|
||||
return True
|
||||
elif (hasattr(types, 'GenericAlias') and # only for py39+
|
||||
isinstance(obj, types.GenericAlias)): # type: ignore
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def safe_getattr(obj: Any, name: str, *defargs: Any) -> Any:
|
||||
"""A getattr() that turns all exceptions into AttributeErrors."""
|
||||
try:
|
||||
|
6
tests/roots/test-ext-autodoc/target/genericalias.py
Normal file
6
tests/roots/test-ext-autodoc/target/genericalias.py
Normal file
@ -0,0 +1,6 @@
|
||||
from typing import List, Callable
|
||||
|
||||
#: A list of int
|
||||
T = List[int]
|
||||
|
||||
C = Callable[[int], None] # a generic alias not having a doccomment
|
@ -560,6 +560,18 @@ def test_isproperty(app):
|
||||
assert inspect.isproperty(func) is False # function
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.version_info < (3, 7), reason='python 3.7+ is required.')
|
||||
@pytest.mark.sphinx(testroot='ext-autodoc')
|
||||
def test_isgenericalias(app):
|
||||
from target.genericalias import C, T
|
||||
from target.methods import Base
|
||||
|
||||
assert inspect.isgenericalias(C) is True
|
||||
assert inspect.isgenericalias(T) is True
|
||||
assert inspect.isgenericalias(object()) is False
|
||||
assert inspect.isgenericalias(Base) is False
|
||||
|
||||
|
||||
def test_unpartial():
|
||||
def func1(a, b, c):
|
||||
pass
|
||||
|
Loading…
Reference in New Issue
Block a user