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

Further improve typing of builtins brain #2225

Merged

Conversation

jacobtylerwalls
Copy link
Member

Type of Changes

Type
🔨 Refactoring

Description

Resolves 12 mypy errors (including all for this module!)

Refs #2214

Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Jun 27, 2023

Codecov Report

Merging #2225 (9cb1eea) into main (efb34f2) will increase coverage by 0.10%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2225      +/-   ##
==========================================
+ Coverage   92.82%   92.93%   +0.10%     
==========================================
  Files          94       95       +1     
  Lines       10836    10921      +85     
==========================================
+ Hits        10059    10149      +90     
+ Misses        777      772       -5     
Flag Coverage Δ
linux 92.73% <100.00%> (+0.10%) ⬆️
pypy 91.07% <100.00%> (-0.18%) ⬇️
windows 92.50% <100.00%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
astroid/nodes/node_classes.py 95.56% <ø> (+0.10%) ⬆️
astroid/typing.py 100.00% <ø> (ø)
astroid/arguments.py 99.24% <100.00%> (ø)
astroid/brain/brain_builtin_inference.py 92.45% <100.00%> (+0.61%) ⬆️
astroid/rebuilder.py 98.31% <100.00%> (+0.05%) ⬆️

... and 8 files with indirect coverage changes

astroid/brain/brain_builtin_inference.py Outdated Show resolved Hide resolved
@@ -257,10 +270,12 @@ def _container_generic_transform(
iterables: tuple[type[nodes.BaseContainer] | type[ContainerObjects], ...],
build_elts: BuiltContainers,
) -> nodes.BaseContainer | None:
elts: Iterable
Copy link
Collaborator

Choose a reason for hiding this comment

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

This contradicts line 301.

Copy link
Member Author

Choose a reason for hiding this comment

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

Line 301 is the klass argument? Having trouble following.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oops, sorry! New code line 300

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it now, thanks!

@@ -399,6 +415,7 @@ def infer_dict(node: nodes.Call, context: InferenceContext | None = None) -> nod
args = call.positional_arguments
kwargs = list(call.keyword_arguments.items())

items: list[tuple[SuccessfulInferenceResult, SuccessfulInferenceResult]]
Copy link
Collaborator

@DanielNoord DanielNoord Jun 27, 2023

Choose a reason for hiding this comment

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

Line 377 of the new line contradicts this, although that might be too broad.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch! We should probably broaden it out. That means this is probably also too narrow:

def postinit(self, elts: list[SuccessfulInferenceResult]) -> None:
self.elts = elts

@@ -1075,11 +1094,13 @@ def _infer_str_format_call(

AstroidManager().register_transform(
nodes.Call,
inference_tip(_infer_copy_method),
inference_tip(_infer_copy_method), # type: ignore[arg-type]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be avoided by using a Generator instead of an Iterator on the signature of this function.

Same goes for the other one, but that then creates other issues.. Can we yield from in that function? That would make it an iterator.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, one more that I'm having trouble following :-)

Elsewhere we return iterators just fine, and they are typed this way:

astroid/astroid/bases.py

Lines 449 to 453 in e3ba1ca

def igetattr(
self, name: str, context: InferenceContext | None = None
) -> Iterator[InferenceResult]:
if name in self.special_attributes:
return iter((self.special_attributes.lookup(name),))

So why would we change to Generator?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Because InferFn expects a Generator. I think because we define it so explicitly we need to do so here as well.. I think we might also get away with changing InferFn to be an Iterator but since we don't actually run mypy that might create issues somewhere else that we don't know about..

Copy link
Member Author

Choose a reason for hiding this comment

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

Ho! Got it now. Okay. I'll audit the uses of InferFn. It's start took like like it should be Iterator.

but since we don't actually run mypy that might create issues somewhere else that we don't know about..

That's why I'm trying to focus by file and make sure with each change the total number goes down and no new issues in the single file I'm focusing on (but yes... whackamole could ensue...)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, that does make the PRs much easier to review.

What I have also noticed is that doing PRs per concept (such as InferFn) is also a good way of grouping things. It might make the big number bigger as the improved typing exposes more issues, but it makes review and reasoning about the issues easier as it is all related.

DanielNoord
DanielNoord previously approved these changes Jun 27, 2023
@jacobtylerwalls
Copy link
Member Author

Thanks for the terrific review!

@jacobtylerwalls jacobtylerwalls merged commit 8d57ce2 into pylint-dev:main Jun 27, 2023
18 checks passed
@jacobtylerwalls jacobtylerwalls deleted the brain-builtin-typing-2 branch June 27, 2023 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants