Skip to content

Use --strict for selfcheck #13464

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 4 commits into from
Aug 26, 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
4 changes: 2 additions & 2 deletions misc/analyze_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def compress(chunk: JsonDict) -> JsonDict:
cache: dict[int, JsonDict] = {}
counter = 0

def helper(chunk: Any) -> Any:
def helper(chunk: JsonDict) -> JsonDict:
nonlocal counter
if not isinstance(chunk, dict):
return chunk
Expand Down Expand Up @@ -121,7 +121,7 @@ def helper(chunk: Any) -> Any:
def decompress(chunk: JsonDict) -> JsonDict:
cache: dict[int, JsonDict] = {}

def helper(chunk: Any) -> Any:
def helper(chunk: JsonDict) -> JsonDict:
if not isinstance(chunk, dict):
return chunk
if ".id" in chunk:
Expand Down
4 changes: 3 additions & 1 deletion misc/incremental_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ def stop_daemon() -> None:
def load_cache(incremental_cache_path: str = CACHE_PATH) -> JsonDict:
if os.path.exists(incremental_cache_path):
with open(incremental_cache_path) as stream:
return json.load(stream)
cache = json.load(stream)
assert isinstance(cache, dict)
return cache
else:
return {}

Expand Down
2 changes: 2 additions & 0 deletions misc/upload-pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ def is_whl_or_tar(name: str) -> bool:
def get_release_for_tag(tag: str) -> dict[str, Any]:
with urlopen(f"{BASE}/{REPO}/releases/tags/{tag}") as f:
data = json.load(f)
assert isinstance(data, dict)
assert data["tag_name"] == tag
return data


def download_asset(asset: dict[str, Any], dst: Path) -> Path:
name = asset["name"]
assert isinstance(name, str)
download_url = asset["browser_download_url"]
assert is_whl_or_tar(name)
with urlopen(download_url) as src_file:
Expand Down
2 changes: 2 additions & 0 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,7 @@ def read_deps_cache(manager: BuildManager, graph: Graph) -> dict[str, FgDepMeta]
return None

module_deps_metas = deps_meta["deps_meta"]
assert isinstance(module_deps_metas, dict)
if not manager.options.skip_cache_mtime_checks:
for id, meta in module_deps_metas.items():
try:
Expand Down Expand Up @@ -1167,6 +1168,7 @@ def _load_json_file(
)
return None
else:
assert isinstance(result, dict)
return result


Expand Down
4 changes: 3 additions & 1 deletion mypy/dmypy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ def run_command(self, command: str, data: dict[str, object]) -> dict[str, object
# Only the above commands use some error formatting.
del data["is_tty"]
del data["terminal_width"]
return method(self, **data)
ret = method(self, **data)
assert isinstance(ret, dict)
return ret

# Command functions (run in the server via RPC).

Expand Down
13 changes: 10 additions & 3 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ def parse(
if raise_on_error and errors.is_errors():
errors.raise_error()

assert isinstance(tree, MypyFile)
return tree


Expand Down Expand Up @@ -1632,7 +1633,9 @@ def visit_ExtSlice(self, n: ast3.ExtSlice) -> TupleExpr:
# Index(expr value)
def visit_Index(self, n: Index) -> Node:
# cast for mypyc's benefit on Python 3.9
return self.visit(cast(Any, n).value)
value = self.visit(cast(Any, n).value)
assert isinstance(value, Node)
return value

# Match(expr subject, match_case* cases) # python 3.10 and later
def visit_Match(self, n: Match) -> MatchStmt:
Expand Down Expand Up @@ -1762,7 +1765,9 @@ def visit(self, node: AST | None) -> ProperType | None:
method = "visit_" + node.__class__.__name__
visitor = getattr(self, method, None)
if visitor is not None:
return visitor(node)
typ = visitor(node)
assert isinstance(typ, ProperType)
return typ
else:
return self.invalid_type(node)
finally:
Expand Down Expand Up @@ -1949,7 +1954,9 @@ def visit_Bytes(self, n: Bytes) -> Type:

def visit_Index(self, n: ast3.Index) -> Type:
# cast for mypyc's benefit on Python 3.9
return self.visit(cast(Any, n).value)
value = self.visit(cast(Any, n).value)
assert isinstance(value, Type)
return value

def visit_Slice(self, n: ast3.Slice) -> Type:
return self.invalid_type(n, note="did you mean to use ',' instead of ':' ?")
Expand Down
4 changes: 3 additions & 1 deletion mypy/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,6 @@ def connection_name(self) -> str:
if sys.platform == "win32":
return self.name
else:
return self.sock.getsockname()
name = self.sock.getsockname()
assert isinstance(name, str)
return name
6 changes: 3 additions & 3 deletions mypy/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def visit_op_expr(self, e: OpExpr) -> Key:
return ("Binary", e.op, literal_hash(e.left), literal_hash(e.right))

def visit_comparison_expr(self, e: ComparisonExpr) -> Key:
rest: Any = tuple(e.operators)
rest: tuple[str | Key | None, ...] = tuple(e.operators)
rest += tuple(literal_hash(o) for o in e.operands)
return ("Comparison",) + rest

Expand All @@ -182,7 +182,7 @@ def visit_unary_expr(self, e: UnaryExpr) -> Key:

def seq_expr(self, e: ListExpr | TupleExpr | SetExpr, name: str) -> Key | None:
if all(literal(x) == LITERAL_YES for x in e.items):
rest: Any = tuple(literal_hash(x) for x in e.items)
rest: tuple[Key | None, ...] = tuple(literal_hash(x) for x in e.items)
return (name,) + rest
return None

Expand All @@ -191,7 +191,7 @@ def visit_list_expr(self, e: ListExpr) -> Key | None:

def visit_dict_expr(self, e: DictExpr) -> Key | None:
if all(a and literal(a) == literal(b) == LITERAL_YES for a, b in e.items):
rest: Any = tuple(
rest: tuple[Key | None, ...] = tuple(
(literal_hash(a) if a else None, literal_hash(b)) for a, b in e.items
)
return ("Dict",) + rest
Expand Down
8 changes: 6 additions & 2 deletions mypy/metastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ def _query(self, name: str, field: str) -> Any:
return results[0][0]

def getmtime(self, name: str) -> float:
return self._query(name, "mtime")
mtime = self._query(name, "mtime")
assert isinstance(mtime, float)
return mtime

def read(self, name: str) -> str:
return self._query(name, "data")
data = self._query(name, "data")
assert isinstance(data, str)
return data

def write(self, name: str, data: str, mtime: float | None = None) -> bool:
import sqlite3
Expand Down
4 changes: 2 additions & 2 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3150,10 +3150,10 @@ class FakeInfo(TypeInfo):
def __init__(self, msg: str) -> None:
self.msg = msg

def __getattribute__(self, attr: str) -> None:
def __getattribute__(self, attr: str) -> type:
# Handle __class__ so that isinstance still works...
if attr == "__class__":
return object.__getattribute__(self, attr)
return object.__getattribute__(self, attr) # type: ignore[no-any-return]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__getattribute__ feels like it could be quite performance-sensitive, so type: ignore feels like the best option here imo.

raise AssertionError(object.__getattribute__(self, "msg"))


Expand Down
4 changes: 2 additions & 2 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def _belongs_to_runtime(r: types.ModuleType, attr: str) -> bool:
except Exception:
return False
if obj_mod is not None:
return obj_mod == r.__name__
return bool(obj_mod == r.__name__)
return not isinstance(obj, types.ModuleType)

runtime_public_contents = (
Expand Down Expand Up @@ -614,7 +614,7 @@ def get_type(arg: Any) -> str | None:

def has_default(arg: Any) -> bool:
if isinstance(arg, inspect.Parameter):
return arg.default != inspect.Parameter.empty
return bool(arg.default != inspect.Parameter.empty)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just curious: why != here does not return bool?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if isinstance(arg, nodes.Argument):
return arg.kind.is_optional()
raise AssertionError
Expand Down
4 changes: 3 additions & 1 deletion mypy/test/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,9 @@ class DataFileCollector(pytest.Collector):
def from_parent( # type: ignore[override]
cls, parent: DataSuiteCollector, *, name: str
) -> DataFileCollector:
return super().from_parent(parent, name=name)
collector = super().from_parent(parent, name=name)
assert isinstance(collector, DataFileCollector)
return collector

def collect(self) -> Iterator[DataDrivenTestCase]:
yield from split_test_cases(
Expand Down
12 changes: 6 additions & 6 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ def __init__(

def accept(self, visitor: TypeVisitor[T]) -> T:
assert isinstance(visitor, SyntheticTypeVisitor)
return visitor.visit_callable_argument(self)
return cast(T, visitor.visit_callable_argument(self))

def serialize(self) -> JsonDict:
assert False, "Synthetic types don't serialize"
Expand All @@ -894,7 +894,7 @@ def __init__(self, items: list[Type], line: int = -1, column: int = -1) -> None:

def accept(self, visitor: TypeVisitor[T]) -> T:
assert isinstance(visitor, SyntheticTypeVisitor)
return visitor.visit_type_list(self)
return cast(T, visitor.visit_type_list(self))

def serialize(self) -> JsonDict:
assert False, "Synthetic types don't serialize"
Expand Down Expand Up @@ -2365,7 +2365,7 @@ def simple_name(self) -> str:

def accept(self, visitor: TypeVisitor[T]) -> T:
assert isinstance(visitor, SyntheticTypeVisitor)
return visitor.visit_raw_expression_type(self)
return cast(T, visitor.visit_raw_expression_type(self))

def serialize(self) -> JsonDict:
assert False, "Synthetic types don't serialize"
Expand Down Expand Up @@ -2488,7 +2488,7 @@ def __init__(self, type: Type, line: int = -1, column: int = -1) -> None:

def accept(self, visitor: TypeVisitor[T]) -> T:
assert isinstance(visitor, SyntheticTypeVisitor)
return visitor.visit_star_type(self)
return cast(T, visitor.visit_star_type(self))

def serialize(self) -> JsonDict:
assert False, "Synthetic types don't serialize"
Expand Down Expand Up @@ -2630,7 +2630,7 @@ class EllipsisType(ProperType):

def accept(self, visitor: TypeVisitor[T]) -> T:
assert isinstance(visitor, SyntheticTypeVisitor)
return visitor.visit_ellipsis_type(self)
return cast(T, visitor.visit_ellipsis_type(self))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you decide between cast and assert? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would generally use assert wherever possible, as it provides runtime verification of the type narrowing. cast feels closer to telling the type checker to shut up and go away 😄

But here, we can't use assert — how can we assert that something is of type T, when we don't know what T is (since T is a TypeVar)? And this also feels like somewhere where, ideally, mypy should be able to infer that the returned type is of type T without any manual type narrowing. So in this specific situation, I'm more at ease with telling the type checker to shut up and go away 😉


def serialize(self) -> JsonDict:
assert False, "Synthetic types don't serialize"
Expand Down Expand Up @@ -2739,7 +2739,7 @@ def __init__(self, fullname: str | None, args: list[Type], line: int) -> None:

def accept(self, visitor: TypeVisitor[T]) -> T:
assert isinstance(visitor, SyntheticTypeVisitor)
return visitor.visit_placeholder_type(self)
return cast(T, visitor.visit_placeholder_type(self))

def serialize(self) -> str:
# We should never get here since all placeholders should be replaced
Expand Down
19 changes: 1 addition & 18 deletions mypy_self_check.ini
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
[mypy]

warn_unused_configs = True
disallow_any_generics = True
disallow_subclassing_any = True
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
check_untyped_defs = True
disallow_untyped_decorators = True
no_implicit_optional = True
warn_redundant_casts = True
warn_unused_ignores = True
no_implicit_reexport = True
strict_equality = True
strict_concatenate = True

; This is the only setting in --strict that we don't have enabled
warn_return_any = False

strict = True
warn_no_return = True
strict_optional = True
disallow_any_unimported = True
Expand Down
2 changes: 1 addition & 1 deletion mypyc/analysis/dataflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(

def __str__(self) -> str:
lines = []
lines.append("exits: %s" % sorted(self.exits, key=lambda e: e.label))
lines.append("exits: %s" % sorted(self.exits, key=lambda e: int(e.label)))
lines.append("succ: %s" % self.succ)
lines.append("pred: %s" % self.pred)
return "\n".join(lines)
Expand Down
4 changes: 3 additions & 1 deletion mypyc/irbuild/constant_fold.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ def constant_fold_binary_int_op(op: str, left: int, right: int) -> int | None:
return left >> right
elif op == "**":
if right >= 0:
return left**right
ret = left**right
assert isinstance(ret, int)
return ret
return None


Expand Down