Skip to content

Commit

Permalink
[partially defined] respect check-untyped-defs flag (#14204)
Browse files Browse the repository at this point in the history
We should not check untyped functions unless --check-untyped-defs is
set.

This is part of the reason why #14166 saw so many new errors reported in
open source projects.
  • Loading branch information
ilinum authored Nov 27, 2022
1 parent 5c3d306 commit 71288c7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ def detect_partially_defined_vars(self, type_map: dict[Expression, Type]) -> Non
manager.errors.set_file(self.xpath, self.tree.fullname, options=manager.options)
self.tree.accept(
PartiallyDefinedVariableVisitor(
MessageBuilder(manager.errors, manager.modules), type_map
MessageBuilder(manager.errors, manager.modules), type_map, manager.options
)
)

Expand Down
8 changes: 7 additions & 1 deletion mypy/partially_defined.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
WithStmt,
implicit_module_attrs,
)
from mypy.options import Options
from mypy.patterns import AsPattern, StarredPattern
from mypy.reachability import ALWAYS_TRUE, infer_pattern_value
from mypy.traverser import ExtendedTraverserVisitor
Expand Down Expand Up @@ -251,9 +252,12 @@ class PartiallyDefinedVariableVisitor(ExtendedTraverserVisitor):
handled by the semantic analyzer.
"""

def __init__(self, msg: MessageBuilder, type_map: dict[Expression, Type]) -> None:
def __init__(
self, msg: MessageBuilder, type_map: dict[Expression, Type], options: Options
) -> None:
self.msg = msg
self.type_map = type_map
self.options = options
self.loops: list[Loop] = []
self.tracker = DefinedVariableTracker()
for name in implicit_module_attrs:
Expand Down Expand Up @@ -329,6 +333,8 @@ def visit_func_def(self, o: FuncDef) -> None:
self.tracker.exit_scope()

def visit_func(self, o: FuncItem) -> None:
if o.is_dynamic() and not self.options.check_untyped_defs:
return
if o.arguments is not None:
for arg in o.arguments:
self.tracker.record_definition(arg.variable.name)
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-partially-defined.test
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,21 @@ def f0() -> None:
# flags: --enable-error-code use-before-def
a = __name__ # No error.
__name__ = "abc"

[case testUntypedDef]
# flags: --enable-error-code partially-defined --enable-error-code use-before-def

def f():
if int():
x = 0
z = y # No use-before-def error because def is untyped.
y = x # No partially-defined error because def is untyped.

[case testUntypedDefCheckUntypedDefs]
# flags: --enable-error-code partially-defined --enable-error-code use-before-def --check-untyped-defs

def f():
if int():
x = 0
z = y # E: Name "y" is used before definition
y: int = x # E: Name "x" may be undefined

0 comments on commit 71288c7

Please sign in to comment.