Skip to content

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Apr 9, 2025

Summary

Basically just repeat the same thing that we did for unresolved-reference, but now for attribute expressions.

We now also handle the case where the unresolved attribute (or the unresolved reference) diagnostic originates from a stringified type annotation.

And I made the evaluation of reachability constraints lazy (will only be evaluated right before we are about to emit a diagnostic).

Test Plan

New Markdown tests for stringified annotations.

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Apr 9, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Apr 9, 2025

mypy_primer results

Changes were detected when running on open source projects
zipp (https://github.com/jaraco/zipp)
+ warning[lint:unused-ignore-comment] /tmp/mypy_primer/projects/zipp/zipp/compat/py310.py:10:23: Unused blanket `type: ignore` directive
- Found 2 diagnostics
+ Found 3 diagnostics

pyp (https://github.com/hauntsaninja/pyp)
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/pyp/pyp.py:604:16: Type `<module 'ast'>` has no attribute `unparse`
- Found 20 diagnostics
+ Found 19 diagnostics

pybind11 (https://github.com/pybind/pybind11)
- dependency graph cycle querying try_metaclass_(Id(26c02)); set cycle_fn/cycle_initial to fixpoint iterate
+ dependency graph cycle querying try_metaclass_(Id(30c04)); set cycle_fn/cycle_initial to fixpoint iterate
- dependency graph cycle querying try_metaclass_(Id(2b012)); set cycle_fn/cycle_initial to fixpoint iterate
+ dependency graph cycle querying try_metaclass_(Id(1e018)); set cycle_fn/cycle_initial to fixpoint iterate
- dependency graph cycle querying member_lookup_with_policy_(Id(1c0aa)); set cycle_fn/cycle_initial to fixpoint iterate
+ dependency graph cycle querying member_lookup_with_policy_(Id(19cf5)); set cycle_fn/cycle_initial to fixpoint iterate

werkzeug (https://github.com/pallets/werkzeug)
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/werkzeug/src/werkzeug/_reloader.py:176:34: Type `<module 'sys'>` has no attribute `orig_argv`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/werkzeug/src/werkzeug/debug/__init__.py:106:22: Type `<module 'winreg'>` has no attribute `OpenKey`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/werkzeug/src/werkzeug/debug/__init__.py:107:21: Type `<module 'winreg'>` has no attribute `HKEY_LOCAL_MACHINE`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/werkzeug/src/werkzeug/debug/__init__.py:110:21: Type `<module 'winreg'>` has no attribute `KEY_READ`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/werkzeug/src/werkzeug/debug/__init__.py:110:39: Type `<module 'winreg'>` has no attribute `KEY_WOW64_64KEY`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/werkzeug/src/werkzeug/debug/__init__.py:114:39: Type `<module 'winreg'>` has no attribute `QueryValueEx`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/werkzeug/src/werkzeug/debug/__init__.py:114:39: Type `<module 'winreg'>` has no attribute `QueryValueEx`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/werkzeug/src/werkzeug/debug/__init__.py:114:39: Type `<module 'winreg'>` has no attribute `QueryValueEx`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/werkzeug/src/werkzeug/debug/__init__.py:116:37: Type `<module 'winreg'>` has no attribute `REG_SZ`
- Found 768 diagnostics
+ Found 759 diagnostics

rich (https://github.com/Textualize/rich)
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/rich/rich/_win32_console.py:12:35: Type `<module 'ctypes'>` has no attribute `WinDLL`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/rich/rich/_windows.py:20:32: Type `<module 'ctypes'>` has no attribute `WinDLL`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/rich/rich/_windows.py:56:27: Type `<module 'sys'>` has no attribute `getwindowsversion`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/rich/rich/traceback.py:464:38: Type `BaseException` has no attribute `exceptions`
- Found 793 diagnostics
+ Found 789 diagnostics

scrapy (https://github.com/scrapy/scrapy)
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/scrapy/tests/CrawlerProcess/asyncio_enabled_reactor_same_loop.py:8:35: Type `<module 'asyncio'>` has no attribute `WindowsSelectorEventLoopPolicy`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/scrapy/tests/CrawlerProcess/asyncio_enabled_reactor_different_loop.py:8:35: Type `<module 'asyncio'>` has no attribute `WindowsSelectorEventLoopPolicy`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/scrapy/scrapy/utils/reactor.py:84:17: Type `<module 'asyncio'>` has no attribute `WindowsSelectorEventLoopPolicy`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/scrapy/scrapy/utils/reactor.py:86:18: Type `<module 'asyncio'>` has no attribute `WindowsSelectorEventLoopPolicy`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/scrapy/tests/test_crawler.py:892:61: Type `<module 'signal'>` has no attribute `SIGBREAK`
- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/scrapy/tests/test_crawler.py:906:61: Type `<module 'signal'>` has no attribute `SIGBREAK`
- Found 1570 diagnostics
+ Found 1564 diagnostics

@MichaReiser
Copy link
Member

The new error in zipp is interesting... It raises questions whether we should raise unused ignore for type: ignore and in which cases

text_encoding = (
    io.text_encoding  # type: ignore[unused-ignore, attr-defined]
    if sys.version_info > (3, 10)
    else _text_encoding
)

@sharkdp sharkdp force-pushed the david/unreachable-attribute-expressions branch 3 times, most recently from 05454c1 to 1a76d8e Compare April 10, 2025 13:45
@sharkdp sharkdp changed the title [red-knot] Silence unresolved-attribute diagnostics [red-knot] Silence unresolved-attribute diagnostics in unreachable code Apr 10, 2025
@sharkdp sharkdp changed the title [red-knot] Silence unresolved-attribute diagnostics in unreachable code [red-knot] Silence unresolved-attribute in unreachable code Apr 10, 2025
@sharkdp sharkdp force-pushed the david/unreachable-attribute-expressions branch from 1a76d8e to 2b51fed Compare April 10, 2025 13:58
@sharkdp sharkdp force-pushed the david/unreachable-attribute-expressions branch from 2b51fed to d1a5619 Compare April 10, 2025 14:03
@sharkdp sharkdp marked this pull request as ready for review April 10, 2025 14:05
@sharkdp
Copy link
Contributor Author

sharkdp commented Apr 10, 2025

The new error in zipp is interesting... It raises questions whether we should raise unused ignore for type: ignore and in which cases

@MichaReiser Is this something specific to this PR, or just a general thought? Anything that should be done about it?

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.

Nice, this LGTM

@sharkdp
Copy link
Contributor Author

sharkdp commented Apr 10, 2025

I manually checked that nothing changed in pybind11, so I'm ignoring the spurious cycle-panic-diff there.

@sharkdp sharkdp merged commit 5b6e949 into main Apr 10, 2025
23 checks passed
@sharkdp sharkdp deleted the david/unreachable-attribute-expressions branch April 10, 2025 15:15
@MichaReiser
Copy link
Member

The new error in zipp is interesting... It raises questions whether we should raise unused ignore for type: ignore and in which cases

@MichaReiser Is this something specific to this PR, or just a general thought? Anything that should be done about it?

I'll create a separate issue for it

dcreager added a commit that referenced this pull request Apr 10, 2025
* main: (30 commits)
  [red-knot] Silence `unresolved-import` in unreachable code (#17336)
  red_knot_python_semantic: move TODO comment
  red_knot_python_semantic: rename `lint()` and `report()`
  ruff_db: use `Annotation::get_message` in more places
  red_knot_python_semantic: tweak docs on building reporter builders
  red_knot_python_semantic: remove the "old" secondary message type
  red_knot_python_semantic: replace one use of "old" secondary diagnostic messages
  red_knot_python_semantic: update revealed type snapshots
  ruff_db: tweak how the revealed type diagnostic is rendered
  red_knot: add explicit test for concise `reveal_type` diagnostic
  red_knot_python_semantic: remove `InferContext::report_diagnostic`
  red_knot_python_semantic: add "reporter" API
  Bump 0.11.5 (#17337)
  [red-knot] Silence `unresolved-attribute` in unreachable code (#17305)
  Revert "[red-knot] Type narrowing for assertions (#17149)" (#17335)
  [red-knot] Type narrowing for assertions (#17149)
  [red-knot] avoid unnecessary evaluation of visibility constraint on definitely-unbound symbol (#17326)
  update cargo-dist (#17325)
  [red-knot] Fix double hovers/inlays in playground (#17334)
  [red-knot] Track reachability of scopes (#17332)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants