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

Avoid false-positives for methods with no return #5

Merged
merged 2 commits into from
Feb 14, 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
12 changes: 10 additions & 2 deletions infer_types/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import pdb # type: ignore[no-redef]


TEST_NAMES = frozenset({'tests.py', 'conftest.py'})


@dataclass(frozen=True)
class Config:
format: bool # run code formatters
Expand Down Expand Up @@ -49,8 +52,13 @@ def add_annotations(root: Path, config: Config) -> None:
continue
if path.suffix != '.py':
continue
if config.skip_tests and path.name.startswith('test_'):
continue
if config.skip_tests:
if path.name.startswith('test_'):
continue
if path.name in TEST_NAMES:
continue
if 'tests' in path.parts:
continue
new_source = inferno.transform(path)
if config.format:
new_source = format_code(new_source)
Expand Down
29 changes: 23 additions & 6 deletions infer_types/_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Extractor = Callable[[astroid.FunctionDef], Type]
extractors: list[tuple[str, Extractor]] = []

UNKNOWN_TYPE = Type.new('')

KNOWN_NAMES = MappingProxyType({
'dumps': 'str',
Expand Down Expand Up @@ -66,7 +67,7 @@ def walk(func_node: astroid.FunctionDef) -> Iterator[astroid.NodeNG]:

@register(name='astypes')
def _extract_astypes(func_node: astroid.FunctionDef) -> Type:
result = Type.new('')
result = UNKNOWN_TYPE
for node in walk(func_node):
if not isinstance(node, astroid.Return):
continue
Expand All @@ -80,7 +81,7 @@ def _extract_astypes(func_node: astroid.FunctionDef) -> Type:
else:
result = result.merge(node_type)
if result.unknown:
return Type.new('')
return UNKNOWN_TYPE
return result


Expand All @@ -91,7 +92,7 @@ def _extract_inherit_method(func_node: astroid.FunctionDef) -> Type:
cls_node = node
break
else:
return Type.new('')
return UNKNOWN_TYPE
for parent in cls_node.getattr(func_node.name):
if isinstance(parent, astroid.BoundMethod):
parent = parent._proxied
Expand Down Expand Up @@ -122,22 +123,38 @@ def _extract_inherit_method(func_node: astroid.FunctionDef) -> Type:
return_type = conv_node_to_type(mod_name, type_node)
if return_type is not None:
return return_type
return Type.new('')
return UNKNOWN_TYPE


@register(name='yield')
def _extract_yield(func_node: astroid.FunctionDef) -> Type:
for node in walk(func_node):
if isinstance(node, (astroid.Yield, astroid.YieldFrom)):
return Type.new('Iterator', module='typing')
return Type.new('')
return UNKNOWN_TYPE


@register(name='none')
def _extract_no_return(func_node: astroid.FunctionDef) -> Type:
# ignore empty methods, they can be there for base class signatures
if isinstance(func_node.parent, astroid.ClassDef):
if not func_node.body:
return UNKNOWN_TYPE
if len(func_node.body) == 1:
node = func_node.body[0]

if isinstance(node, (astroid.Raise, astroid.Pass)):
return UNKNOWN_TYPE
if isinstance(node, astroid.Expr):
node = node.value
if isinstance(node, astroid.Const) and node.value == ...:
return UNKNOWN_TYPE

for node in walk(func_node):
if isinstance(node, (astroid.Yield, astroid.YieldFrom)):
return UNKNOWN_TYPE
if isinstance(node, astroid.Return) and node.value is not None:
return Type.new('')
return UNKNOWN_TYPE
return Type.new('None')


Expand Down
58 changes: 58 additions & 0 deletions tests/test_inferno.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,64 @@ class C:
def m(self, x) -> Iterator:
yield 12
""",
# don't infer no return for empty base class methods
"""
class A:
def m(self, x):
raise NotImplementedError
---
class A:
def m(self, x):
raise NotImplementedError
""",
"""
class A:
def m(self, x):
"some docstring"
---
class A:
def m(self, x):
"some docstring"
""",
"""
class A:
def m(self, x):
pass
---
class A:
def m(self, x):
pass
""",
"""
class A:
def m(self, x):
...
---
class A:
def m(self, x):
...
""",
# infer no return for methods
"""
class A:
def m(self, x):
pass
1 + 2
---
class A:
def m(self, x) -> None:
pass
1 + 2
""",
"""
class A:
def m(self, x):
13
---
class A:
def m(self, x) -> None:
13
""",
])
def test_inferno(transform, fused: str) -> None:
given, expected = fused.split('---')
Expand Down