Skip to content

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Nov 14, 2025

Summary

A simpler alternative to #19412, that gets rid of lots of false positives.

We now treat if foo.bar.baz.spam.eggs.TYPE_CHECKING the same as if TYPE_CHECKING.

Test Plan

Added mdtests

Ecosystem impact

Lots of invalid-return-type false positives go away for things like this:

import typing as t

if t.TYPE_CHECKING:
    def f() -> str: ...

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Nov 14, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 14, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 14, 2025

mypy_primer results

Changes were detected when running on open source projects
bidict (https://github.com/jab/bidict)
- bidict/_bidict.py:41:30: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `MutableBidict[VT@MutableBidict, KT@MutableBidict]`
- bidict/_bidict.py:44:26: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `MutableBidict[VT@MutableBidict, KT@MutableBidict]`
- bidict/_bidict.py:185:30: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `bidict[VT@bidict, KT@bidict]`
- bidict/_bidict.py:188:26: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `bidict[VT@bidict, KT@bidict]`
- bidict/_frozen.py:34:30: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `frozenbidict[VT@frozenbidict, KT@frozenbidict]`
- bidict/_frozen.py:37:26: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `frozenbidict[VT@frozenbidict, KT@frozenbidict]`
- bidict/_orderedbase.py:138:30: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `OrderedBidictBase[VT@OrderedBidictBase, KT@OrderedBidictBase]`
- bidict/_orderedbase.py:141:26: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `OrderedBidictBase[VT@OrderedBidictBase, KT@OrderedBidictBase]`
- bidict/_orderedbidict.py:39:30: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `OrderedBidict[VT@OrderedBidict, KT@OrderedBidict]`
- bidict/_orderedbidict.py:42:26: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `OrderedBidict[VT@OrderedBidict, KT@OrderedBidict]`
- Found 25 diagnostics
+ Found 15 diagnostics

spack (https://github.com/spack/spack)
- lib/spack/spack/vendor/jinja2/bccache.py:26:36: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `bytes`
- lib/spack/spack/vendor/jinja2/ext.py:27:44: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
- lib/spack/spack/vendor/jinja2/ext.py:30:67: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
- lib/spack/spack/vendor/jinja2/ext.py:34:59: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
- lib/spack/spack/vendor/jinja2/ext.py:37:82: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
- lib/spack/spack/vendor/jinja2/filters.py:38:31: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
- lib/spack/spack/vendor/jinja2/runtime.py:40:14: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
- lib/spack/spack/vendor/markupsafe/__init__.py:10:31: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
- Found 7911 diagnostics
+ Found 7903 diagnostics

jinja (https://github.com/pallets/jinja)
- src/jinja2/ext.py:34:59: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
- src/jinja2/ext.py:38:14: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
- Found 181 diagnostics
+ Found 179 diagnostics

meson (https://github.com/mesonbuild/meson)
- mesonbuild/compilers/d.py:110:44: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `list[str]`
- mesonbuild/linkers/linkers.py:634:68: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `list[str]`
- mesonbuild/linkers/linkers.py:1372:68: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `list[str]`
- mesonbuild/options.py:402:36: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `int`
- Found 1734 diagnostics
+ Found 1730 diagnostics

No memory usage changes detected ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 14, 2025

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-return-type 0 24 0
Total 0 24 0

Full report with detailed diff (timing results)

@AlexWaygood AlexWaygood marked this pull request as ready for review November 14, 2025 13:06
Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thank you!

@AlexWaygood AlexWaygood merged commit 5f50137 into main Nov 14, 2025
40 checks passed
@AlexWaygood AlexWaygood deleted the alex/type-checking-attributes branch November 14, 2025 13:11
dcreager added a commit that referenced this pull request Nov 14, 2025
* origin/main: (59 commits)
  [ty] Improve diagnostic range for `non-subscriptable` diagnostics (#21461)
  [ty] Improve literal promotion heuristics (#21439)
  [ty] Further improve details around which expressions should be deferred in stub files (#21456)
  [ty] Improve generic class constructor inference (#21442)
  [ty] Propagate type context through conditional expressions (#21443)
  [ty] Suppress completions when introducing names with `as`
  [ty] Add panic-by-default await methods to `TestServer` (#21451)
  [ty] name is parameter and global is a syntax error (#21312)
  [ty] Fixup a few details around version-specific dataclass features (#21453)
  [ty] Support attribute-expression `TYPE_CHECKING` conditionals (#21449)
  [ty] Support stringified annotations in value-position `Annotated` instances (#21447)
  [ty] Type inference for genererator expressions (#21437)
  [ty] Make `__getattr__` available for `ModuleType` instances (#21450)
  [ty] Increase default receive timeout in tests to 10s (#21448)
  [ty] Add synthetic members to completions on dataclasses (#21446)
  [ty] Support legacy `typing` special forms in implicit type aliases (#21433)
  Bump 0.14.5 (#21435)
  [ty] Support `type[…]` and `Type[…]` in implicit type aliases (#21421)
  [ty] Respect notebook cell boundaries when adding an auto import (#21322)
  Update PyCharm setup instructions (#21409)
  ...
dcreager added a commit that referenced this pull request Nov 14, 2025
* dcreager/deep-comparison: (64 commits)
  assuming
  SubtypingAssuming
  implies_subtype_of
  name tweak
  Apply suggestions from code review
  [ty] Improve diagnostic range for `non-subscriptable` diagnostics (#21461)
  [ty] Improve literal promotion heuristics (#21439)
  [ty] Further improve details around which expressions should be deferred in stub files (#21456)
  [ty] Improve generic class constructor inference (#21442)
  [ty] Propagate type context through conditional expressions (#21443)
  [ty] Suppress completions when introducing names with `as`
  [ty] Add panic-by-default await methods to `TestServer` (#21451)
  [ty] name is parameter and global is a syntax error (#21312)
  [ty] Fixup a few details around version-specific dataclass features (#21453)
  [ty] Support attribute-expression `TYPE_CHECKING` conditionals (#21449)
  [ty] Support stringified annotations in value-position `Annotated` instances (#21447)
  [ty] Type inference for genererator expressions (#21437)
  [ty] Make `__getattr__` available for `ModuleType` instances (#21450)
  [ty] Increase default receive timeout in tests to 10s (#21448)
  [ty] Add synthetic members to completions on dataclasses (#21446)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem-analyzer ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants