Skip to content

Commit 1977545

Browse files
committed
ci: Type-check with ty
1 parent 8efecdc commit 1977545

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+277
-267
lines changed

config/ty.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[src]
2+
exclude = ["tests/fixtures"]
3+
4+
[terminal]
5+
error-on-warning = true
6+
output-format = "concise"

docs/guide/users/loading.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ Traceback (most recent call last):
218218
resolved = self.modules_collection.get_member(self.target_path)
219219
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
220220
File "griffe/_internal/mixins.py", line 84, in get_member
221-
return self.members[parts[0]].get_member(parts[1:]) # type: ignore[attr-defined]
221+
return self.members[parts[0]].get_member(parts[1:])
222222
~~~~~~~~~~~~^^^^^^^^^^
223223
KeyError: 'package1'
224224

duties.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _pyprefix(title: str) -> str:
4141
def _get_changelog_version() -> str:
4242
changelog_version_re = re.compile(r"^## \[(\d+\.\d+\.\d+)\].*$")
4343
with Path(__file__).parent.joinpath("CHANGELOG.md").open("r", encoding="utf8") as file:
44-
return next(filter(bool, map(changelog_version_re.match, file))).group(1) # ty: ignore[invalid-argument-type]
44+
return next(filter(bool, map(changelog_version_re.match, file))).group(1) # ty:ignore[invalid-argument-type]
4545

4646

4747
@duty
@@ -215,7 +215,7 @@ def check_types(ctx: Context) -> None:
215215
as a last resort you can ignore this specific error with a comment:
216216
217217
```python title="src/your_package/module.py"
218-
print("a code line that triggers a ty warning") # ty: ignore[ID]
218+
print("a code line that triggers a ty warning") # ty:ignore[ID]
219219
```
220220
221221
...where ID is the name of the warning.
@@ -235,7 +235,7 @@ def check_types(ctx: Context) -> None:
235235
Now add a comment to ignore this warning.
236236
237237
```python title="src/your_package/module.py"
238-
result = data_dict.get(key, None).value # ty: ignore[possibly-missing-attribute]
238+
result = data_dict.get(key, None).value # ty:ignore[possibly-missing-attribute]
239239
```
240240
241241
```console
@@ -246,8 +246,12 @@ def check_types(ctx: Context) -> None:
246246
"""Check that the code is correctly typed."""
247247
py = f"{sys.version_info.major}.{sys.version_info.minor}"
248248
ctx.run(
249-
tools.ty.check(*PY_SRC_LIST, color=True, error_on_warning=True, python_version=py),
250-
title=_pyprefix("Type-checking"),
249+
tools.ty.check(
250+
*PY_SRC_LIST,
251+
config_file="config/ty.toml",
252+
color=True,
253+
python_version=py,
254+
),
251255
)
252256

253257

@@ -517,6 +521,6 @@ def test_seed(seed: int, revisit: bool = False) -> bool: # noqa: FBT001,FBT002
517521
return True
518522

519523
revisit = bool(seeds)
520-
seeds = seeds or sample(range(min_seed, max_seed + 1), size) # type: ignore[assignment]
524+
seeds = seeds or sample(range(min_seed, max_seed + 1), size) # ty:ignore[invalid-assignment]
521525
for seed in seeds:
522526
ctx.run(test_seed, args=[seed, revisit], title=f"Visiting code generated with seed {seed}")

packages/griffecli/src/griffecli/_internal/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def dump(
414414
if per_package_output:
415415
for package_name, data in data_packages.items():
416416
serialized = data.as_json(indent=2, full=full, sort_keys=True)
417-
_print_data(serialized, output.format(package=package_name)) # type: ignore[union-attr]
417+
_print_data(serialized, output.format(package=package_name)) # ty:ignore[possibly-missing-attribute]
418418
else:
419419
serialized = json.dumps(data_packages, cls=JSONEncoder, indent=2, full=full, sort_keys=True)
420420
_print_data(serialized, output)

packages/griffelib/src/griffe/_internal/agents/inspector.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def __init__(
156156
self.parent: Module | None = parent
157157
"""An optional parent for the final module object."""
158158

159-
self.current: Module | Class = None # type: ignore[assignment]
159+
self.current: Module | Class = None
160160
"""The current object being inspected."""
161161

162162
self.docstring_parser: DocstringStyle | Parser | None = docstring_parser
@@ -351,7 +351,7 @@ def inspect_class(self, node: ObjectNode) -> None:
351351
self.generic_inspect(node)
352352
self.extensions.call("on_members", node=node, obj=class_, agent=self)
353353
self.extensions.call("on_class_members", node=node, cls=class_, agent=self)
354-
self.current = self.current.parent # type: ignore[assignment]
354+
self.current = self.current.parent # ty:ignore[invalid-assignment]
355355

356356
def inspect_staticmethod(self, node: ObjectNode) -> None:
357357
"""Inspect a static method.
@@ -561,7 +561,7 @@ def handle_attribute(self, node: ObjectNode, annotation: str | Expr | None = Non
561561
elif parent.kind is Kind.FUNCTION:
562562
if parent.name != "__init__":
563563
return
564-
parent = parent.parent # type: ignore[assignment]
564+
parent = parent.parent
565565
labels.add("instance-attribute")
566566

567567
try:
@@ -581,10 +581,10 @@ def handle_attribute(self, node: ObjectNode, annotation: str | Expr | None = Non
581581
analysis="dynamic",
582582
)
583583
attribute.labels |= labels
584-
parent.set_member(node.name, attribute)
584+
parent.set_member(node.name, attribute) # ty:ignore[possibly-missing-attribute]
585585

586586
if node.name == "__all__":
587-
parent.exports = list(node.obj)
587+
parent.exports = list(node.obj) # ty:ignore[invalid-assignment]
588588
self.extensions.call("on_instance", node=node, obj=attribute, agent=self)
589589
self.extensions.call("on_attribute_instance", node=node, attr=attribute, agent=self)
590590

@@ -642,7 +642,7 @@ def _convert_object_to_annotation(obj: Any, *, parent: Module | Class, member: s
642642
annotation_node = compile(annotation, mode="eval", filename="<>", flags=ast.PyCF_ONLY_AST, optimize=2)
643643
except SyntaxError:
644644
return obj
645-
return safe_get_annotation(annotation_node.body, parent, member=member) # type: ignore[attr-defined]
645+
return safe_get_annotation(annotation_node.body, parent, member=member) # ty:ignore[unresolved-attribute]
646646

647647

648648
_type_parameter_kind_map = {
@@ -674,9 +674,9 @@ def _convert_type_parameters(
674674
if bound is not None:
675675
bound = _convert_type_to_annotation(bound, parent=parent, member=member)
676676
constraints: list[str | Expr] = [
677-
_convert_type_to_annotation(constraint, parent=parent, member=member) # type: ignore[misc]
677+
_convert_type_to_annotation(constraint, parent=parent, member=member)
678678
for constraint in getattr(type_parameter, "__constraints__", ())
679-
]
679+
] # ty:ignore[invalid-assignment]
680680

681681
if getattr(type_parameter, "has_default", lambda: False)():
682682
default = _convert_type_to_annotation(
@@ -711,10 +711,10 @@ def _convert_type_to_annotation(obj: Any, *, parent: Module | Class, member: str
711711
]
712712

713713
if origin is types.UnionType:
714-
return functools.reduce(lambda left, right: ExprBinOp(left, "|", right), args) # type: ignore[arg-type]
714+
return functools.reduce(lambda left, right: ExprBinOp(left, "|", right), args)
715715

716716
origin = _convert_type_to_annotation(origin, parent=parent, member=member)
717717
if origin is None:
718718
return None
719719

720-
return ExprSubscript(origin, ExprTuple(args, implicit=True)) # type: ignore[arg-type]
720+
return ExprSubscript(origin, ExprTuple(args, implicit=True)) # ty:ignore[invalid-argument-type]

packages/griffelib/src/griffe/_internal/agents/nodes/ast.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ def ast_children(node: AST) -> Iterator[AST]:
3838
except AttributeError:
3939
continue
4040
if isinstance(field, AST):
41-
field.parent = node # type: ignore[attr-defined]
41+
field.parent = node
4242
yield field
4343
elif isinstance(field, list):
4444
for child in field:
4545
if isinstance(child, AST):
46-
child.parent = node # type: ignore[attr-defined]
46+
child.parent = node
4747
yield child
4848

4949

@@ -56,7 +56,7 @@ def ast_previous_siblings(node: AST) -> Iterator[AST]:
5656
Yields:
5757
The previous siblings.
5858
"""
59-
for sibling in ast_children(node.parent): # type: ignore[attr-defined]
59+
for sibling in ast_children(node.parent): # ty:ignore[unresolved-attribute]
6060
if sibling is not node:
6161
yield sibling
6262
else:
@@ -72,7 +72,7 @@ def ast_next_siblings(node: AST) -> Iterator[AST]:
7272
Yields:
7373
The next siblings.
7474
"""
75-
siblings = ast_children(node.parent) # type: ignore[attr-defined]
75+
siblings = ast_children(node.parent) # ty:ignore[unresolved-attribute]
7676
for sibling in siblings:
7777
if sibling is node:
7878
break
@@ -88,7 +88,7 @@ def ast_siblings(node: AST) -> Iterator[AST]:
8888
Yields:
8989
The siblings.
9090
"""
91-
siblings = ast_children(node.parent) # type: ignore[attr-defined]
91+
siblings = ast_children(node.parent) # ty:ignore[unresolved-attribute]
9292
for sibling in siblings:
9393
if sibling is not node:
9494
yield sibling

packages/griffelib/src/griffe/_internal/agents/nodes/docstrings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def get_docstring(
2222
# TODO: Possible optimization using a type map.
2323
if isinstance(node, ast.Expr):
2424
doc = node.value
25-
elif not strict and node.body and isinstance(node.body, list) and isinstance(node.body[0], ast.Expr): # type: ignore[attr-defined]
26-
doc = node.body[0].value # type: ignore[attr-defined]
25+
elif not strict and node.body and isinstance(node.body, list) and isinstance(node.body[0], ast.Expr): # ty:ignore[unresolved-attribute]
26+
doc = node.body[0].value # ty:ignore[unresolved-attribute]
2727
else:
2828
return None, None, None
2929
if isinstance(doc, ast.Constant) and isinstance(doc.value, str):

packages/griffelib/src/griffe/_internal/agents/nodes/exports.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _extract_binop(node: ast.BinOp, parent: Module) -> list[str | ExprName]:
2626

2727

2828
def _extract_constant(node: ast.Constant, parent: Module) -> list[str | ExprName]:
29-
return [node.value] # type: ignore[list-item]
29+
return [node.value] # ty:ignore[invalid-return-type]
3030

3131

3232
def _extract_name(node: ast.Name, parent: Module) -> list[str | ExprName]:

packages/griffelib/src/griffe/_internal/agents/nodes/imports.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def relative_to_absolute(node: ast.ImportFrom, name: ast.alias, current_module:
2525
if (level > 0 and current_module.is_package) or current_module.is_subpackage:
2626
level -= 1
2727
while level > 0 and current_module.parent is not None:
28-
current_module = current_module.parent # type: ignore[assignment]
28+
current_module = current_module.parent # ty:ignore[invalid-assignment]
2929
level -= 1
3030
base = current_module.path + "." if node.level > 0 else ""
3131
node_module = node.module + "." if node.module else ""

packages/griffelib/src/griffe/_internal/agents/nodes/runtime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def module_path(self) -> str | None:
113113
except ValueError:
114114
return None
115115
try:
116-
return module.__spec__.name # type: ignore[union-attr]
116+
return module.__spec__.name # ty:ignore[possibly-missing-attribute]
117117
except AttributeError:
118118
return getattr(module, "__name__", None)
119119
else:

0 commit comments

Comments
 (0)