Skip to content

Rename partially-defined error codes #14267

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

Merged
merged 2 commits into from
Dec 10, 2022
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
14 changes: 7 additions & 7 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from mypy.indirection import TypeIndirectionVisitor
from mypy.messages import MessageBuilder
from mypy.nodes import Import, ImportAll, ImportBase, ImportFrom, MypyFile, SymbolTable, TypeInfo
from mypy.partially_defined import PartiallyDefinedVariableVisitor
from mypy.partially_defined import PossiblyUndefinedVariableVisitor
from mypy.semanal import SemanticAnalyzer
from mypy.semanal_pass1 import SemanticAnalyzerPreAnalysis
from mypy.util import (
Expand Down Expand Up @@ -2347,18 +2347,18 @@ def type_check_second_pass(self) -> bool:
self.time_spent_us += time_spent_us(t0)
return result

def detect_partially_defined_vars(self, type_map: dict[Expression, Type]) -> None:
def detect_possibly_undefined_vars(self, type_map: dict[Expression, Type]) -> None:
assert self.tree is not None, "Internal error: method must be called on parsed file only"
if self.tree.is_stub:
# We skip stub files because they aren't actually executed.
return
manager = self.manager
if manager.errors.is_error_code_enabled(
codes.PARTIALLY_DEFINED
) or manager.errors.is_error_code_enabled(codes.USE_BEFORE_DEF):
codes.POSSIBLY_UNDEFINED
) or manager.errors.is_error_code_enabled(codes.USED_BEFORE_DEF):
manager.errors.set_file(self.xpath, self.tree.fullname, options=manager.options)
self.tree.accept(
PartiallyDefinedVariableVisitor(
PossiblyUndefinedVariableVisitor(
MessageBuilder(manager.errors, manager.modules), type_map, manager.options
)
)
Expand Down Expand Up @@ -3412,7 +3412,7 @@ def process_stale_scc(graph: Graph, scc: list[str], manager: BuildManager) -> No
graph[id].type_check_first_pass()
if not graph[id].type_checker().deferred_nodes:
unfinished_modules.discard(id)
graph[id].detect_partially_defined_vars(graph[id].type_map())
graph[id].detect_possibly_undefined_vars(graph[id].type_map())
graph[id].finish_passes()

while unfinished_modules:
Expand All @@ -3421,7 +3421,7 @@ def process_stale_scc(graph: Graph, scc: list[str], manager: BuildManager) -> No
continue
if not graph[id].type_check_second_pass():
unfinished_modules.discard(id)
graph[id].detect_partially_defined_vars(graph[id].type_map())
graph[id].detect_possibly_undefined_vars(graph[id].type_map())
graph[id].finish_passes()
for id in stale:
graph[id].generate_unused_ignore_notes()
Expand Down
8 changes: 4 additions & 4 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ def __str__(self) -> str:
ANNOTATION_UNCHECKED = ErrorCode(
"annotation-unchecked", "Notify about type annotations in unchecked functions", "General"
)
PARTIALLY_DEFINED: Final[ErrorCode] = ErrorCode(
"partially-defined",
POSSIBLY_UNDEFINED: Final[ErrorCode] = ErrorCode(
"possibly-undefined",
"Warn about variables that are defined only in some execution paths",
"General",
default_enabled=False,
Expand Down Expand Up @@ -192,8 +192,8 @@ def __str__(self) -> str:
"General",
default_enabled=False,
)
USE_BEFORE_DEF: Final[ErrorCode] = ErrorCode(
"use-before-def",
USED_BEFORE_DEF: Final[ErrorCode] = ErrorCode(
"used-before-def",
"Warn about variables that are used before they are defined",
"General",
default_enabled=False,
Expand Down
4 changes: 2 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,10 +1229,10 @@ def undefined_in_superclass(self, member: str, context: Context) -> None:
self.fail(f'"{member}" undefined in superclass', context)

def variable_may_be_undefined(self, name: str, context: Context) -> None:
self.fail(f'Name "{name}" may be undefined', context, code=codes.PARTIALLY_DEFINED)
self.fail(f'Name "{name}" may be undefined', context, code=codes.POSSIBLY_UNDEFINED)

def var_used_before_def(self, name: str, context: Context) -> None:
self.fail(f'Name "{name}" is used before definition', context, code=codes.USE_BEFORE_DEF)
self.fail(f'Name "{name}" is used before definition', context, code=codes.USED_BEFORE_DEF)

def first_argument_for_super_must_be_type(self, actual: Type, context: Context) -> None:
actual = get_proper_type(actual)
Expand Down
20 changes: 10 additions & 10 deletions mypy/partially_defined.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def skip_branch(self) -> None:
assert len(self.branches) > 0
self.branches[-1].skipped = True

def is_partially_defined(self, name: str) -> bool:
def is_possibly_undefined(self, name: str) -> bool:
assert len(self.branches) > 0
return name in self.branches[-1].may_be_defined

Expand Down Expand Up @@ -213,10 +213,10 @@ def pop_undefined_ref(self, name: str) -> set[NameExpr]:
assert len(self.scopes) > 0
return self._scope().pop_undefined_ref(name)

def is_partially_defined(self, name: str) -> bool:
def is_possibly_undefined(self, name: str) -> bool:
assert len(self._scope().branch_stmts) > 0
# A variable is undefined if it's in a set of `may_be_defined` but not in `must_be_defined`.
return self._scope().branch_stmts[-1].is_partially_defined(name)
return self._scope().branch_stmts[-1].is_possibly_undefined(name)

def is_defined_in_different_branch(self, name: str) -> bool:
"""This will return true if a variable is defined in a branch that's not the current branch."""
Expand All @@ -243,7 +243,7 @@ def __init__(self) -> None:
self.has_break = False


class PartiallyDefinedVariableVisitor(ExtendedTraverserVisitor):
class PossiblyUndefinedVariableVisitor(ExtendedTraverserVisitor):
"""Detects the following cases:
- A variable that's defined only part of the time.
- If a variable is used before definition
Expand All @@ -253,7 +253,7 @@ class PartiallyDefinedVariableVisitor(ExtendedTraverserVisitor):
x = 1
print(x) # Error: "x" may be undefined.

Example of a use before definition:
Example of a used before definition:
x = y
y: int = 2

Expand All @@ -273,15 +273,15 @@ def __init__(
self.tracker.record_definition(name)

def var_used_before_def(self, name: str, context: Context) -> None:
if self.msg.errors.is_error_code_enabled(errorcodes.USE_BEFORE_DEF):
if self.msg.errors.is_error_code_enabled(errorcodes.USED_BEFORE_DEF):
self.msg.var_used_before_def(name, context)

def variable_may_be_undefined(self, name: str, context: Context) -> None:
if self.msg.errors.is_error_code_enabled(errorcodes.PARTIALLY_DEFINED):
if self.msg.errors.is_error_code_enabled(errorcodes.POSSIBLY_UNDEFINED):
self.msg.variable_may_be_undefined(name, context)

def process_definition(self, name: str) -> None:
# Was this name previously used? If yes, it's a use-before-definition error.
# Was this name previously used? If yes, it's a used-before-definition error.
refs = self.tracker.pop_undefined_ref(name)
for ref in refs:
self.var_used_before_def(name, ref)
Expand Down Expand Up @@ -471,7 +471,7 @@ def visit_starred_pattern(self, o: StarredPattern) -> None:
def visit_name_expr(self, o: NameExpr) -> None:
if refers_to_builtin(o):
return
if self.tracker.is_partially_defined(o.name):
if self.tracker.is_possibly_undefined(o.name):
# A variable is only defined in some branches.
self.variable_may_be_undefined(o.name, o)
# We don't want to report the error on the same variable multiple times.
Expand All @@ -488,7 +488,7 @@ def visit_name_expr(self, o: NameExpr) -> None:
# 2. The variable is defined later in the code.
# Case (1) will be caught by semantic analyzer. Case (2) is a forward ref that should
# be caught by this visitor. Save the ref for later, so that if we see a definition,
# we know it's a use-before-definition scenario.
# we know it's a used-before-definition scenario.
self.tracker.record_undefined_ref(o)
super().visit_name_expr(o)

Expand Down
2 changes: 1 addition & 1 deletion mypy/server/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def restore(ids: list[str]) -> None:
state.type_checker().reset()
state.type_check_first_pass()
state.type_check_second_pass()
state.detect_partially_defined_vars(state.type_map())
state.detect_possibly_undefined_vars(state.type_map())
t2 = time.time()
state.finish_passes()
t3 = time.time()
Expand Down
Loading