Skip to content

Commit 949ff7d

Browse files
committed
fix: Never raise alias resolution error when resolving a name from an __init__ method scope
Issue-374: #374
1 parent b537ab9 commit 949ff7d

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/_griffe/models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,8 +2100,11 @@ def resolve(self, name: str) -> str:
21002100
obj = self.modules_collection.get_member(resolved)
21012101
except KeyError:
21022102
return resolved
2103-
if obj.is_attribute and "instance-attribute" in obj.labels:
2104-
raise NameResolutionError(name)
2103+
try:
2104+
if obj.is_attribute and "instance-attribute" in obj.labels:
2105+
raise NameResolutionError(name)
2106+
except AliasResolutionError:
2107+
pass
21052108
return resolved
21062109
return super().resolve(name)
21072110

tests/test_models.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,21 @@ def __init__(self):
539539
""",
540540
) as module:
541541
assert module["A.x"].value.canonical_path == "x" # Not `module.A.x`.
542+
543+
544+
def test_resolving_never_raises_alias_errors() -> None:
545+
"""Resolving never raises alias errors."""
546+
with temporary_visited_package(
547+
"package",
548+
{
549+
"__init__.py": """
550+
from package.mod import pd
551+
552+
class A:
553+
def __init__(self):
554+
pass
555+
""",
556+
"mod.py": "import pandas as pd",
557+
},
558+
) as module:
559+
assert module["A.__init__"].resolve("pd") == "package.mod.pd"

0 commit comments

Comments
 (0)