in packages/python-packages/apiview-stub-generator/apistub/nodes/_base_node.py [0:0]
def get_qualified_name(obj, namespace: str) -> str:
"""Generate and return fully qualified name of object with module name for internal types.
If module name is not available for the object then it will return name
:param: obj
Parameter object of type class, function or enum
"""
module_name = getattr(obj, "__module__", "")
if module_name.startswith("astroid"):
return obj.as_string()
elif module_name == "types":
return str(obj)
if obj is Parameter.empty:
return None
name = str(obj)
if hasattr(obj, "__name__"):
name = getattr(obj, "__name__")
elif hasattr(obj, "__qualname__"):
name = getattr(obj, "__qualname__")
wrap_optional = False
args = []
# newer versions of Python extract inner types into __args__
# and are no longer part of the name
if hasattr(obj, "__args__"):
for arg in obj.__args__ or []:
arg_string = str(arg)
if keyword_regex.match(arg_string):
value = keyword_regex.search(arg_string).group(2)
if value == "NoneType":
# we ignore NoneType since Optional implies that NoneType is
# acceptable
if not name.startswith("Optional"):
wrap_optional = True
else:
args.append(value)
elif forward_ref_regex.match(arg_string):
value = forward_ref_regex.search(arg_string).group(1)
args.append(value)
else:
args.append(arg_string)
# omit any brackets for Python 3.9/3.10 compatibility
value = name_regex.search(name).group(0)
if module_name and module_name.startswith(namespace):
value = f"{module_name}.{name}"
elif module_name and module_name != value and value.startswith(module_name):
# strip the module name if it isn't the namespace (example: typing)
value = value[len(module_name) + 1 :]
if args and "[" not in value:
arg_string = ", ".join(args)
value = f"{value}[{arg_string}]"
if wrap_optional:
value = f"Optional[{value}]"
return value