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

Don't flag intentionally empty generators unreachable #15722

Conversation

ikonst
Copy link
Contributor

@ikonst ikonst commented Jul 20, 2023

Fixes #15345.

@ikonst
Copy link
Contributor Author

ikonst commented Jul 20, 2023

👋 @asottile

@github-actions

This comment has been minimized.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thanks for working on this!

test-data/unit/check-unreachable-code.test Show resolved Hide resolved
mypy/checker.py Outdated
@@ -1063,6 +1064,20 @@ def enter_attribute_inference_context(self) -> Iterator[None]:
yield None
self.inferred_attribute_types = old_types

def _is_empty_generator(self, func: FuncItem) -> bool:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def _is_empty_generator(self, func: FuncItem) -> bool:
@staticmethod
def _is_empty_generator(func: FuncItem) -> bool:

Or even better: move the function into the global scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mypy/checker.py Outdated
@@ -1240,7 +1255,7 @@ def check_func_def(
# have no good way of doing this.
#
# TODO: Find a way of working around this limitation
if len(expanded) >= 2:
if len(expanded) >= 2 or self._is_empty_generator(item):
Copy link
Member

Choose a reason for hiding this comment

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

Maybe update the multiline comment immediately above this line? We're not just suppressing reachability warnings for TypeVars with value restrictions anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I didn't want to muddy the blame for that comment 🤣

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ikonst ikonst force-pushed the 07-19-intentionally_empty_generator_marked_as_unreachable branch from e31efd3 to 5e33f47 Compare July 20, 2023 18:39
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

teeny tiny nits

mypy/checker.py Outdated
Comment on lines 6973 to 6974
return (
len(body := func.body.body) == 2
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think the first walrus here is slightly gratuitous

Suggested change
return (
len(body := func.body.body) == 2
body = func.body.body
return (
len(body) == 2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mypy/checker.py Outdated
Comment on lines 1245 to 1246
# We suppress reachability warnings for empty generators (return; yield), since there's
# no way to promote a function into a generator except by adding an "unreachable" yield.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# We suppress reachability warnings for empty generators (return; yield), since there's
# no way to promote a function into a generator except by adding an "unreachable" yield.
# We also suppress reachability warnings for empty generator functions
# (return; yield), since the most idiomatic way to promote a function into a
# generator function is often to add an "unreachable" yield.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had "also" at first, but I wanted to be able to remove the first comment w/o touching the second.

Copy link
Member

Choose a reason for hiding this comment

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

Meh, I think I'd personally prioritise a readable comment over a tiny bit more churn in git blame :p

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, easy. I've just made the "TODO remove me" part second. :P

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

mypy/checker.py Outdated Show resolved Hide resolved
mypy/checker.py Outdated Show resolved Hide resolved
test-data/unit/check-unreachable-code.test Outdated Show resolved Hide resolved
test-data/unit/check-unreachable-code.test Outdated Show resolved Hide resolved
mypy/checker.py Outdated Show resolved Hide resolved
ikonst and others added 2 commits July 20, 2023 17:53
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
mypy/checker.py Outdated Show resolved Hide resolved
@github-actions

This comment has been minimized.

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@github-actions

This comment has been minimized.

@ikonst
Copy link
Contributor Author

ikonst commented Jul 23, 2023

@AlexWaygood I removed the comment in a090d56 since now this field appears to have a long-term legitimate use.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

mypy/checker.py Outdated Show resolved Hide resolved
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

graphql-core (https://github.com/graphql-python/graphql-core): typechecking got 1.05x slower (366.9s -> 386.6s)
(Performance measurements are based on a single noisy sample)

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thankss!

@hauntsaninja hauntsaninja merged commit 01c6994 into python:master Jul 26, 2023
17 checks passed
@ikonst ikonst deleted the 07-19-intentionally_empty_generator_marked_as_unreachable branch July 26, 2023 12:06
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.

intentionally empty generator marked as unreachable
5 participants