crisp: fix many type errors found by mypy and ty - #11
Conversation
spernsteiner
left a comment
There was a problem hiding this comment.
This is very helpful—thank you!
| c_code: NodeId | ||
| cmd: list[str] | ||
| exit_code: int | ||
| compile_commands: Optional[NodeId] | ||
|
|
||
| c_code = property(lambda self: self._metadata["c_code"]) | ||
| cmd = property(lambda self: self._metadata["cmd"]) | ||
| exit_code = property(lambda self: self._metadata["exit_code"]) | ||
| compile_commands = property(lambda self: self._metadata["compile_commands"]) | ||
| @property | ||
| def c_code(self) -> NodeId: | ||
| return self._metadata["c_code"] |
There was a problem hiding this comment.
IIRC there's logic somewhere that looks at the field type annotations on Node subclasses—will that work properly with the new style of field declarations?
There was a problem hiding this comment.
Oh, I'm not sure. Let me see. It might have to be adjusted, but the previous way didn't type-check (the field declaration is different from the field = property(), which is function).
There was a problem hiding this comment.
I tried this approach and it seems to work:
def node_prop(name: str):
return property(lambda self: self._metadata[name])
class FindUnsafeAnalysisNode(Node):
KIND = "find_unsafe_analysis"
code: NodeId = node_prop('code')
commit: str = node_prop('commit')
stderr: str = node_prop('stderr')
# `body` stores the JSON outputLess verbose, and it keeps typing.get_type_hints working as before. mypy and ty check both accept it—they don't complain about the assignment in the class definition (e.g. code = node_prop('code')), and do complain about misuse of field values, like this:
def test_mypy(x: FindUnsafeAnalysisNode) -> int:
return x.stderr
# ^ error: Incompatible return value type (got "str", expected "int")AIUI this works specifically because node_prop has no annotation on its return type, so mypy/ty won't check the code = node_prop('code') assignment. If you annotate node_prop with -> property then both tools complain about the assignment.
Previously, we were using `property` calls with lambdas and field annotations, which isn't allowed. Idiomatic `@property` methods are not much more verbose and work.
`pygit2.init_repository`'s 2nd arg is `bare: bool = False`, but we were passing `flags: RepositoryInitFlag`.
d82778a to
96e7594
Compare
|
I tested this locally and ran into some errors about I also hit an error about recursive imports in |
kkysen
left a comment
There was a problem hiding this comment.
Okay I can take a look. I still need to rebase it, though, as things got pretty out of date.
This adds the
mypy(and associated*-typespackages needed to help it) andtytype checkers as dev dependencies and fixes almost all of the type errors found by both of them (under non-strict mode). It doesn't add them to CI yet, though, as there are still some errors that I'm not sure how to resolve. But fixing some of these first is helpful, especially 58717b8, as that uncovered a bug. These can be run locally withuv run mypy crispanduv run ty check.I think this should supersede most of #1, as the code has changed a lot since then, and what type checker was being used there wasn't totally clear.