-
-
Notifications
You must be signed in to change notification settings - Fork 280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to type NodeNG.parent
#2017
Comments
You could try making Something like this. Note that I needed to remove the from __future__ import annotations
from typing import Generic, Any
from typing_extensions import TypeVar
_Parent = TypeVar(
"_Parent" #, bound="LocalsDictNode | None", default="LocalsDictNode"
)
class Node(Generic[_Parent]):
def __init__(self, parent: _Parent) -> None:
self.parent = parent
class LocalsDictNode(Node[_Parent]):
...
class Module(LocalsDictNode[None]):
def __init__(self) -> None:
super().__init__(None)
self.parent = None
reveal_type(self.parent)
def method(self) -> None:
reveal_type(self.parent)
class FunctionDef(LocalsDictNode[Node[LocalsDictNode[Any | None]]]):
def method(self) -> None:
if isinstance(self.parent, Module):
reveal_type(self.parent.parent) # None
else:
reveal_type(self.parent.parent) # LocalsDictNode[Any | None] |
Another suggestion would be to use a |
Yeah I thought about the recursiveness, but that didn't really feel good as well.. I wonder if |
The above code almost meets all requirements we would have for
.parent
except for one. I'll list them in the hopes of getting any good ideas on how to fix this. Note that this uses the proposed PEP 696 as I saw no other way to even get this far without.Node
itself should not need any type parameters (because of its genericness) and should be useable without anyNodeNG.parent
should be aLocalsDictNode
LocalsDictNode.parent
should also be aLocalsDictNode
Module.parent
. That should beNone
.The above design almost meets that requirement except for that in
FunctionDef.method the second
reveal_typeshows
LocalsDictNode | None, which should be
LocalsDictNodeas we know that
node.parentisn't a
Moduleand therefore
node.parent.parentshould be
LocalsDictNode`./CC @cdce8p as you might have a good idea for this. Hopefully...
The text was updated successfully, but these errors were encountered: