Skip to content

Add stronger typing to nodes.py #1783

Closed
@rwbarton

Description

@rwbarton

We should have a subclass Expression of Node, make all the *Expr node types inherit from it, and make all the fields of Nodes that are intended to be expressions into Expressions. Similarly for Statement, and possibly Var and related types.

NodeVisitor should be parameterized on at least two types, namely the return type on expressions and the return type on other things (mostly statements). Node's accept method should be moved into Expression and Statement (etc.), returning different type parameters of the NodeVisitor. Then TypeChecker can become a NodeVisitor[Type, None], meaning that it returns a type on an expression but returns nothing on a statement.

Then TypeChecker's visit_*_stmt methods wouldn't all be returning None where a Type was expected. So this is pretty important for getting mypy to type check itself under strict optional checking, which was my original motivation, but I think this refactoring is a good idea anyways: more specific types for the fields of Node subtypes give more information about what values are actually legal, which is helpful when either producing or consuming those nodes.

I don't want to do this refactoring right now as it's fairly large, and the parser is one of the things heavily affected, and we currently have two of those. Better to wait until after the old parser is gone.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions