-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
Should we change how attributes are defined on certain nodes classes? #1250
Comments
I've already spend some time working on this exact issue 😅 |
I'll dim down the typing issues for now 😄 Let me know if you need any help! |
Don't panic, there's enough issues for everyone 😄 |
I'm going to take another try at the |
@DanielNoord You don't need to fix the dataclass brain to fix the issue. It's much easier. See (2) here: pylint-dev/pylint#5321 (comment). Just haven't had the time to prepare a PR with tests. The brain fix can be done later. |
Yeah I know, but I thought I would try and do them both. We can use the tests I already wrote in that case 😄 I'll try again and if I don't succeed I'll create a PR with the fix in (2). |
(2) should be addressed even if you find the issue 😄 |
I ran into this while working on the typing errors that we need to solve before turning on
mypy
. A similar issue was encountered in https://github.com/PyCQA/astroid/pull/1244/files.Some of our
nodes
classes have attributes that get defined in apostinit
. Take for examplefunc
onnodes.Call
:https://github.com/PyCQA/astroid/blob/907818246c89ef938b23448e0cecc52b7c8693e0/astroid/nodes/node_classes.py#L1635-L1642
https://github.com/PyCQA/astroid/blob/907818246c89ef938b23448e0cecc52b7c8693e0/astroid/nodes/node_classes.py#L1660-L1664
We know that this
func
attribute can't technically beNone
. If it wereNone
theCall
wouldn't have a function to call. In thepostinit
you can clearly see the difference between attributes that can and can not beNone
. For example,args
andkeywords
can beNone
. (They should probably be[]
in that case to allow iteration, but that's another issue).I wonder what would happen if we were to change
self.func: Optional[NodeNG] = None
toself.func: NodeNG
. This would simplify a lot of our typing as we wouldn't need to add checks for the existence ofCall.func
everywhere. Perhaps there are solutions to this which we should consider, but I think there must be a way to show thatnodes.Call
must have afunc
attribute, but that we don't know it at initialisation time.One issue I already thought of is what would happen if something accessed
func
before thepostinit
is called. I don't think this is an issue and any exception raised is correct: the instance shouldn't be accessed before thepostinit
is complete.I have created DanielNoord#3 to show that this does indeed pass our test suite. The added
ValueError
is there to immediately break a test.My proposal would be to identify all of these non-optional attributes and change their typing.
I'm pinging @cdce8p as you often have valuable insights for typing related issues.
The text was updated successfully, but these errors were encountered: