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

Avoid false unreachable and redundant-expr warnings in loops. #18433

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tyralla
Copy link
Collaborator

@tyralla tyralla commented Jan 9, 2025

Fixes #18348
Fixes #13973
Fixes #11612
Fixes #8721
Fixes #8865
Fixes #7204

I manually checked all the listed issues. Some of them were already partly fixed by #18180.

Fixes python#18348
Fixes python#13973
Fixes python#11612
Fixes python#8721
Fixes python#8865
Fixes python#7204

I manually checked all the listed issues.  Some of them were already partly fixed by python#18180.

This comment has been minimized.

@tyralla
Copy link
Collaborator Author

tyralla commented Jan 9, 2025

Such a long primer diff. It seems like I enabled redundant-expr by accident or something like that. I will check it tomorrow.

mypy/checker.py Outdated
Comment on lines 613 to 617
if warn_unreachable or warn_redundant:
self.options.warn_unreachable = warn_unreachable
self.options.enabled_error_codes.add(codes.REDUNDANT_EXPR)
with self.binder.frame_context(can_skip=True, break_frame=2, continue_frame=1):
self.accept(body)
Copy link
Collaborator

@hauntsaninja hauntsaninja Jan 10, 2025

Choose a reason for hiding this comment

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

Suggested change
if warn_unreachable or warn_redundant:
self.options.warn_unreachable = warn_unreachable
self.options.enabled_error_codes.add(codes.REDUNDANT_EXPR)
with self.binder.frame_context(can_skip=True, break_frame=2, continue_frame=1):
self.accept(body)
if warn_unreachable:
self.options.warn_unreachable = warn_unreachable
if warn_redundant:
self.options.enabled_error_codes.add(codes.REDUNDANT_EXPR)
if warn_unreachable or warn_redundant:
with self.binder.frame_context(can_skip=True, break_frame=2, continue_frame=1):
self.accept(body)

Maybe something like this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, thank you, that should work. I wrote slightly differently yesterday but then lost my internet connection and now pushed it before seeing this comment. If you prefer your version (and can accept the general solution), please feel free to edit the section before merging.

Copy link
Contributor

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

bandersnatch (https://github.com/pypa/bandersnatch)
+ src/bandersnatch_storage_plugins/s3.py:62: error: Unused "type: ignore" comment  [unused-ignore]

core (https://github.com/home-assistant/core)
+ homeassistant/components/recorder/migration.py:2283: error: Unused "type: ignore" comment  [unused-ignore]
+ homeassistant/components/recorder/migration.py:2305: error: Unused "type: ignore" comment  [unused-ignore]
+ homeassistant/components/schedule/__init__.py:76: error: Unused "type: ignore" comment  [unused-ignore]
+ homeassistant/components/twentemilieu/calendar.py:73: error: Unused "type: ignore" comment  [unused-ignore]

pyodide (https://github.com/pyodide/pyodide)
+ src/py/pyodide/_state.py:21: error: Incompatible types in assignment (expression has type "pyodide._state.<subclass of "ModuleType" and "JsProxy">2", target has type "pyodide._state.<subclass of "ModuleType" and "JsProxy">")  [assignment]

@tyralla
Copy link
Collaborator Author

tyralla commented Jan 10, 2025

Regarding pypa/bandersnatch and home-assistant/core, the diff looks good. Regarding pyodide/pyodide, I neither understand why this error is emitted now nor why it is emitted in general (confusing error message). Maybe you know this behaviour already. I could investigate later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment