pytorch/torch/package/analyze/is_from_package.py
Yuanyuan Chen 70925bdf82 [1/N] Use "is" in python type comparison (#165037)
It generally recommended to use `is/is not` to compare types. Therefore this series of changes apply this suggestion in the code base, and it aims to finally enabling related linter checks.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/165037
Approved by: https://github.com/mlazos
2025-10-10 12:36:50 +00:00

17 lines
404 B
Python

from types import ModuleType
from typing import Any
from .._mangling import is_mangled
def is_from_package(obj: Any) -> bool:
"""
Return whether an object was loaded from a package.
Note: packaged objects from externed modules will return ``False``.
"""
if type(obj) is ModuleType:
return is_mangled(obj.__name__)
else:
return is_mangled(type(obj).__module__)