Skip to content

Commit

Permalink
refactor: Remove useless branch in resolve method, add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Sep 3, 2024
1 parent 58eb9f4 commit aa6c7e4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/_griffe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,17 +774,12 @@ def resolve(self, name: str) -> str:
return self.members[name].target_path # type: ignore[union-attr]
return self.members[name].path

# Name was imported.
if name in self.imports:
return self.imports[name]

# Name unknown and no more parent scope.
if self.parent is None:
# could be a built-in
raise NameResolutionError(f"{name} could not be resolved in the scope of {self.path}")

# Name is parent, non-module object.
# NOTE: possibly useless branch.
if name == self.parent.name and not self.parent.is_module:
return self.parent.path

Expand Down
50 changes: 50 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Docstring,
GriffeLoader,
Module,
NameResolutionError,
module_vtree,
temporary_inspected_module,
temporary_pypackage,
Expand Down Expand Up @@ -443,3 +444,52 @@ class Derived(Base):
derived_params = [p.name for p in module["Derived"].parameters]
assert base_params == ["self", "a"]
assert derived_params == ["self", "a", "d"]


def test_name_resolution() -> None:
"""Name are correctly resolved in the scope of an object."""
code = """
module_attribute = 0
class Class:
import imported
class_attribute = 0
def __init__(self):
self.instance_attribute = 0
def method(self):
local_variable = 0
"""
with temporary_visited_module(code) as module:
assert module.resolve("module_attribute") == "module.module_attribute"
assert module.resolve("Class") == "module.Class"

assert module["module_attribute"].resolve("Class") == "module.Class"
with pytest.raises(NameResolutionError):
module["module_attribute"].resolve("class_attribute")

assert module["Class"].resolve("module_attribute") == "module.module_attribute"
assert module["Class"].resolve("imported") == "imported"
assert module["Class"].resolve("class_attribute") == "module.Class.class_attribute"
assert module["Class"].resolve("instance_attribute") == "module.Class.instance_attribute"
assert module["Class"].resolve("method") == "module.Class.method"

assert module["Class.class_attribute"].resolve("module_attribute") == "module.module_attribute"
assert module["Class.class_attribute"].resolve("Class") == "module.Class"
assert module["Class.class_attribute"].resolve("imported") == "imported"
assert module["Class.class_attribute"].resolve("instance_attribute") == "module.Class.instance_attribute"
assert module["Class.class_attribute"].resolve("method") == "module.Class.method"

assert module["Class.instance_attribute"].resolve("module_attribute") == "module.module_attribute"
assert module["Class.instance_attribute"].resolve("Class") == "module.Class"
assert module["Class.instance_attribute"].resolve("imported") == "imported"
assert module["Class.instance_attribute"].resolve("class_attribute") == "module.Class.class_attribute"
assert module["Class.instance_attribute"].resolve("method") == "module.Class.method"

assert module["Class.method"].resolve("module_attribute") == "module.module_attribute"
assert module["Class.method"].resolve("Class") == "module.Class"
assert module["Class.method"].resolve("imported") == "imported"
assert module["Class.method"].resolve("class_attribute") == "module.Class.class_attribute"
assert module["Class.method"].resolve("instance_attribute") == "module.Class.instance_attribute"

0 comments on commit aa6c7e4

Please sign in to comment.