Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested classes in stubs #647

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix crash with nested classes in stubs. For now, `Any` is
inferred for such classes (#647)
- Add `disallowed_imports` configuration option to disallow
imports of specific modules (#645, #646)
- Consider an annotated assignment without a value to be
Expand Down
3 changes: 3 additions & 0 deletions pyanalyze/stubs/_pyanalyze_tests-stubs/nested.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Outer:
class Inner:
def __init__(self, x: int) -> None: ...
30 changes: 30 additions & 0 deletions pyanalyze/test_typeshed.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,33 @@ def capybara():
assert_is_value(
itertools.count(1), GenericValue(itertools.count, [TypedValue(int)])
)


class TestNestedClass(TestNameCheckVisitorBase):
@assert_passes()
def test_nested(self):
def capybara() -> None:
from _pyanalyze_tests.nested import Outer

Outer.Inner(1)

@assert_passes()
def test_with_runtime_object(self):
import sys
import types

class Inner:
def __init__(self, arg: int) -> None:
pass

# The bug here only reproduces if a class that exists at runtime contains
# a nested class in a stub. So we simulate that by creating a fake runtime
# module.
Outer = type("Outer", (), {"Inner": Inner})
Outer.__module__ = "_pyanalyze_tests.nested"
mod = types.ModuleType("_pyanalyze_tests.nested")
mod.Outer = Outer
sys.modules["_pyanalyze_tests.nested"] = mod

def capybara() -> None:
Outer.Inner(1)
16 changes: 14 additions & 2 deletions pyanalyze/typeshed.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ def _get_attribute_from_info(
mod,
is_typeddict=is_typeddict,
on_class=on_class,
parent_name=info.ast.name,
)
assert False, repr(child_info)
return UNINITIALIZED_VALUE
Expand All @@ -514,6 +515,7 @@ def _get_value_from_child_info(
*,
is_typeddict: bool,
on_class: bool,
parent_name: str,
) -> Value:
if isinstance(node, ast.AnnAssign):
return self._parse_type(node.annotation, mod, is_typeddict=is_typeddict)
Expand All @@ -530,12 +532,21 @@ def _get_value_from_child_info(
return AnyValue(AnySource.inference)
else:
return CallableValue(sig)
elif isinstance(node, ast.ClassDef):
# should be
# SubclassValue(TypedValue(f"{mod}.{parent_name}.{node.name}"), exactly=True)
# but that doesn't currently work well
return AnyValue(AnySource.inference)
elif isinstance(node, ast.Assign):
return UNINITIALIZED_VALUE
elif isinstance(node, typeshed_client.OverloadedName):
vals = [
self._get_value_from_child_info(
subnode, mod, is_typeddict=is_typeddict, on_class=on_class
subnode,
mod,
is_typeddict=is_typeddict,
on_class=on_class,
parent_name=parent_name,
)
for subnode in node.definitions
]
Expand Down Expand Up @@ -1204,7 +1215,8 @@ def _value_from_info_inner(

def _obj_from_qualname_is(module_name: str, qualname: str, obj: object) -> bool:
try:
__import__(module_name)
if module_name not in sys.modules:
__import__(module_name)
mod = sys.modules[module_name]
actual = mod
for piece in qualname.split("."):
Expand Down