Add basic typechecking#72
Conversation
…nstead of mro() (Improvement of ~15% in name resolution)
…ng type info to nodes (So I don't have to create yet another tree of node classes and another set of converters)
TODO: AST types, return AST rather than True/False
`__notes__` isn't present any more
|
Ok, so need to fix the snapshot-fmt to be same as before for the untyped AST. |
… or all versions)
TODO: should stop supporting 3.10 soon, by November at the latest
(Note: this and last commit were TDD'd together - well the TDD was used for bug fixes)
…ems to be quite effective
|
Gah, I can't wait for 3.10 to go end-of-life so I can do typing properly (well as properly as you can in the current state of the type system) |
(More 3.10 weirdness...)
How on Earth did fuzzing not catch this?
FAILING(typecheck(errors)): on error region when multiple
…ehaviour not crashing
|
Look at that diff! Well it's only |
Because Pycharm now somewhat supports `TypeVar` defaults (I mean that type-checking doesn't completely die because of the presence of `default`). Also, added `typing_extensions` up to 3.12 for `TypeVar` with default to requirements-dev.txt (dev because only needed for type checking)
MarcellPerger1
left a comment
There was a problem hiding this comment.
Lots of comments.
| return result | ||
|
|
||
| @classmethod | ||
| def _get_funcs(cls, mapping: dict[type[WT] | type, list[VT]], tp: type[WT]) -> list[VT]: |
There was a problem hiding this comment.
type[WT] | type - why can't this simply be type[WT]? Do we have to replace type[WT] with WTT here? Hopefully not, hopefully type[WT] will just work but Pycharm bugs.
|
|
||
| class FilteredWalker(BasicFilteredWalker[WT], Generic[WT]): | ||
| def __init__(self): | ||
| # Can then add studd to specific instances |
There was a problem hiding this comment.
| # Can then add studd to specific instances | |
| # Can then add stuff to specific instances |
| The restrictions on name are because we have no other way of detecting | ||
| it (without metaclass dark magic) as we can't refer to the class while | ||
| its namespace is being evaluated |
There was a problem hiding this comment.
We could just do __init_subclass__ with some slight dark magic though... like iterating over the newly defined attributes and seeing which one is of our new fancy custom type that can only be created via the decorator? Or like setting an attribute on the functions a la @abstractmethod
| class HasRegion: | ||
| region: StrRegion |
There was a problem hiding this comment.
Should this not be a Protocol? PR does Pycharm not support that properly?
|
|
||
| from parser.cst.cstgen import CstGen | ||
| from parser.lexer.tokenizer import Tokenizer | ||
| from scripts.fuzz import fuzz |
There was a problem hiding this comment.
Are we sure we want to have tests importing scripts while some scripts use tests?
| decl: AstDeclNode[TypeMetadata] | ||
| self.assertTypecheckedTo(decl.value, ValType()) | ||
| self.assertTypecheckedTo(decl.ident, ValType()) | ||
| # self.assertMatchesSnapshot(prog) |
There was a problem hiding this comment.
Should we un-comment these?
There was a problem hiding this comment.
I'm not sure there's much point because all we're changing in the typechecker is the types, so snapshots would only test the astgen as extra.
| from parser.common.error import BaseLocatedError | ||
|
|
||
|
|
||
| class _PatchRegionDisplay: |
There was a problem hiding this comment.
A comment on why we need this would be good. Also, I recall there's some BoundMock in some other file. Would that work here?
| def __init__(self): | ||
| def _mocked_display(*args, **kwargs): | ||
| return self._mock(*args, **kwargs) | ||
| self._mock = MagicMock() |
There was a problem hiding this comment.
A comment about what this mock is etc would be good (this bit is quite non-obvious)
| # (Note: dataclasses._MISSING_TYPE isn't actually a runtime thing, it's just | ||
| # for type checkers to recognise dataclasses.MISSING) |
There was a problem hiding this comment.
Comment in wrong place, should be 3 lines up.
For now the typechecking is very basic and proof-of-concept. We may need to create a typed AST.