Skip to content

Add basic typechecking#72

Open
MarcellPerger1 wants to merge 94 commits into
mainfrom
add-typechecking
Open

Add basic typechecking#72
MarcellPerger1 wants to merge 94 commits into
mainfrom
add-typechecking

Conversation

@MarcellPerger1

@MarcellPerger1 MarcellPerger1 commented Jun 15, 2025

Copy link
Copy Markdown
Owner
  • Added basic typechecking
  • Resolve TODOs in code
  • Split into several modules
  • Add a few unittests
  • Add e2e test using example 2 (will require some modifications though)
  • Add fuzzing

For now the typechecking is very basic and proof-of-concept. We may need to create a typed AST.

@MarcellPerger1 MarcellPerger1 added T-feature A new feature A-TypeChecking Area: Type checking/name resolution labels Jun 15, 2025
@MarcellPerger1

Copy link
Copy Markdown
Owner Author

Ok, so need to fix the snapshot-fmt to be same as before for the untyped AST.

@MarcellPerger1

Copy link
Copy Markdown
Owner Author

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)

@MarcellPerger1
MarcellPerger1 marked this pull request as ready for review July 14, 2026 16:47
@MarcellPerger1

Copy link
Copy Markdown
Owner Author

Look at that diff! Well it's only +2000 -800 coz snapshots but still!

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 MarcellPerger1 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Lots of comments.

Comment thread parser/astgen/ast_node.py
return result

@classmethod
def _get_funcs(cls, mapping: dict[type[WT] | type, list[VT]], tp: type[WT]) -> list[VT]:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Suggested change
# Can then add studd to specific instances
# Can then add stuff to specific instances

Comment on lines +165 to +167
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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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

Comment thread parser/common/common.py
Comment on lines 13 to 14
class HasRegion:
region: StrRegion

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Should we un-comment these?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread test/test_error.py
from parser.common.error import BaseLocatedError


class _PatchRegionDisplay:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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?

Comment thread test/test_error.py
def __init__(self):
def _mocked_display(*args, **kwargs):
return self._mock(*args, **kwargs)
self._mock = MagicMock()

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

A comment about what this mock is etc would be good (this bit is quite non-obvious)

Comment thread util/__init__.py
Comment on lines +48 to +49
# (Note: dataclasses._MISSING_TYPE isn't actually a runtime thing, it's just
# for type checkers to recognise dataclasses.MISSING)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Comment in wrong place, should be 3 lines up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-TypeChecking Area: Type checking/name resolution T-feature A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant