Skip to content
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

Add Arguments typing to Lambda & FunctionDef #1174

Merged
merged 2 commits into from Sep 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 5 additions & 19 deletions astroid/nodes/scoped_nodes.py
Expand Up @@ -69,7 +69,7 @@
from astroid.interpreter.dunder_lookup import lookup
from astroid.interpreter.objectmodel import ClassModel, FunctionModel, ModuleModel
from astroid.manager import AstroidManager
from astroid.nodes import Const, node_classes
from astroid.nodes import Arguments, Const, node_classes

ITER_METHODS = ("__iter__", "__getitem__")
EXCEPTION_BASE_CLASSES = frozenset({"Exception", "BaseException"})
Expand Down Expand Up @@ -1190,7 +1190,6 @@ def type(self):
:returns: 'method' if this is a method, 'function' otherwise.
:rtype: str
"""
# pylint: disable=no-member
if self.args.arguments and self.args.arguments[0].name == "self":
if isinstance(self.parent.scope(), ClassDef):
return "method"
Expand All @@ -1214,11 +1213,8 @@ def __init__(self, lineno=None, col_offset=None, parent=None):
:type: dict(str, NodeNG)
"""

self.args = []
"""The arguments that the function takes.

:type: Arguments or list
"""
self.args: Arguments
Copy link
Member

Choose a reason for hiding this comment

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

At some point we should probably enforce that postinit is actually called. Otherwise self.args might not be set when accessed. Task for another time. (It's somewhere on my long list.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is already note the case.. Of the two places creating a FunctioDef only one does this. Oh well... 😄

Copy link
Member

@cdce8p cdce8p Sep 15, 2021

Choose a reason for hiding this comment

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

Which one do you mean? raw_building, objectmodel, and rebuilder are fine. Am I missing something? I didn't notice any errors while running the tests either.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

https://github.com/PyCQA/astroid/blob/9a7878a78a284571d3788afc42203089185cfcbf/astroid/raw_building.py#L127-L150

The postinit is called on Arguments, but the FunctionDef.postinit method is never called.

Copy link
Member

Choose a reason for hiding this comment

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

At least func.args is still assigned. So it won't break (just yet).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Something to fix in the future I guess. Seems like the Lambda and FunctionDef class could use a refactor anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Someday... Too many other things open at the moment 😪

Copy link
Member

Choose a reason for hiding this comment

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

Someday... Too many other things open at the moment

Yeah let's finish up 2.11 first and the typing MR first. The code is getting better little by little and that's great, but let's merge what we did already. There are a lot of changes in those, and conflicts could become a problem.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I expect to have pylint-dev/pylint#4980 ready for review in 5-10 minutes!

"""The arguments that the function takes."""

self.body = []
"""The contents of the function body.
Expand All @@ -1228,11 +1224,10 @@ def __init__(self, lineno=None, col_offset=None, parent=None):

super().__init__(lineno, col_offset, parent)

def postinit(self, args, body):
def postinit(self, args: Arguments, body):
"""Do some setup after initialisation.

:param args: The arguments that the function takes.
:type args: Arguments

:param body: The contents of the function body.
:type body: list(NodeNG)
Expand Down Expand Up @@ -1276,10 +1271,6 @@ def argnames(self):
:returns: The names of the arguments.
:rtype: list(str)
"""
# pylint: disable=no-member; github.com/pycqa/astroid/issues/291
# args is in fact redefined later on by postinit. Can't be changed
# to None due to a strong interaction between Lambda and FunctionDef.

if self.args.arguments: # maybe None with builtin functions
names = _rec_get_names(self.args.arguments)
else:
Expand Down Expand Up @@ -1319,10 +1310,6 @@ def scope_lookup(self, node, name, offset=0):
globals or builtin).
:rtype: tuple(str, list(NodeNG))
"""
# pylint: disable=no-member; github.com/pycqa/astroid/issues/291
# args is in fact redefined later on by postinit. Can't be changed
# to None due to a strong interaction between Lambda and FunctionDef.

if node in self.args.defaults or node in self.args.kw_defaults:
frame = self.parent.frame()
# line offset to avoid that def func(f=func) resolve the default
Expand Down Expand Up @@ -1440,7 +1427,7 @@ def __init__(self, name=None, doc=None, lineno=None, col_offset=None, parent=Non
# pylint: disable=arguments-differ; different than Lambdas
def postinit(
self,
args,
args: Arguments,
body,
decorators=None,
returns=None,
Expand All @@ -1450,7 +1437,6 @@ def postinit(
"""Do some setup after initialisation.

:param args: The arguments that the function takes.
:type args: Arguments or list

:param body: The contents of the function body.
:type body: list(NodeNG)
Expand Down