Skip to content

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Oct 13, 2025

Summary

Treat functions, methods, and dynamic types as function-like Callables

closes astral-sh/ty#1342
closes astral-sh/ty#1344

Ecosystem analysis

All removed diagnostics look like cases of astral-sh/ty#1344

Test Plan

Added regression test

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Oct 13, 2025
@sharkdp sharkdp changed the title [ty] Treat functions, methods, and dynamic types as function-like Cal… [ty] Treat functions, methods, and dynamic types as function-like Callables Oct 13, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Oct 13, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

github-actions bot commented Oct 13, 2025

mypy_primer results

Changes were detected when running on open source projects
attrs (https://github.com/python-attrs/attrs)
- src/attr/validators.py:175:31: error[unresolved-attribute] Type `(Overload[(pattern: str | Pattern[str], string: str, flags: Unknown = int) -> Match[str] | None, (pattern: bytes | Pattern[bytes], string: @Todo, flags: Unknown = int) -> Match[bytes] | None]) & ~AlwaysFalsy` has no attribute `__name__`
- tests/test_validators.py:870:13: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | ((val: _T@lt) -> @Todo) | ((val: _T@le) -> @Todo) | ((val: _T@ge) -> @Todo) | ((val: _T@gt) -> @Todo)` may be missing
- Found 561 diagnostics
+ Found 559 diagnostics

spack (https://github.com/spack/spack)
- lib/spack/spack/vendor/attr/validators.py:186:31: error[unresolved-attribute] Type `(Overload[(pattern: str | Pattern[str], string: str, flags: Unknown = int) -> Match[str] | None, (pattern: bytes | Pattern[bytes], string: @Todo, flags: Unknown = int) -> Match[bytes] | None]) & ~AlwaysFalsy` has no attribute `__name__`
- Found 7521 diagnostics
+ Found 7520 diagnostics

mitmproxy (https://github.com/mitmproxy/mitmproxy)
- examples/contrib/block_dns_over_https.py:357:19: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | ((flow) -> Unknown)` may be missing
- Found 1843 diagnostics
+ Found 1842 diagnostics

vision (https://github.com/pytorch/vision)
- test/test_transforms_v2.py:6756:40: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | ((inpt: Unknown) -> Unknown) | ((inpt: Unknown) -> int) | ((pic, mode=None) -> Unknown) | ((inpt: Unknown, displacement: Unknown, interpolation: InterpolationMode | int = InterpolationMode, fill: @Todo = None) -> Unknown)` may be missing
- Found 1482 diagnostics
+ Found 1481 diagnostics

openlibrary (https://github.com/internetarchive/openlibrary)
- openlibrary/records/driver.py:61:36: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | ((params) -> Unknown)` may be missing
- Found 849 diagnostics
+ Found 848 diagnostics

jax (https://github.com/google/jax)
- jax/experimental/jax2tf/tests/primitives_test.py:146:30: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | ((x: @Todo, y: @Todo, /) -> Array)` may be missing
- Found 2427 diagnostics
+ Found 2426 diagnostics

sympy (https://github.com/sympy/sympy)
- sympy/benchmarks/bench_symbench.py:134:31: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | (() -> Unknown)` may be missing
- Found 14028 diagnostics
+ Found 14027 diagnostics

materialize (https://github.com/MaterializeInc/materialize)
- test/aws/mzcompose.py:109:34: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | ((c: Unknown, ctx: TestContext) -> Unknown)` may be missing
- Found 3387 diagnostics
+ Found 3386 diagnostics

scipy (https://github.com/scipy/scipy)
- scipy/optimize/tests/test_zeros.py:539:43: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | ((f, a, b, args=tuple[()], xtol=float, rtol=Unknown, maxiter=int, full_output=bool, disp=bool) -> Unknown)` may be missing
- scipy/signal/tests/test_filter_design.py:4131:16: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | ((N, Wn, btype=str, analog=bool, output=str, norm=str, fs=None) -> Unknown) | ((N, Wn, btype=str, analog=bool, output=str, fs=None) -> Unknown) | ... omitted 3 union elements` may be missing
- scipy/signal/tests/test_windows.py:798:25: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | ((M, *, sym=bool, xp=None, device=None) -> Unknown) | ((M, at, sym=bool, *, xp=None, device=None) -> Unknown) | ... omitted 6 union elements` may be missing
- scipy/sparse/_base.py:676:55: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | ((a: @Todo, b: @Todo, /) -> Any)` may be missing
- scipy/special/_precompute/gammainc_data.py:117:36: warning[possibly-missing-attribute] Attribute `__name__` on type `Unknown | ((a, x, dps=int, maxterms=int) -> Unknown)` may be missing
- Found 6861 diagnostics
+ Found 6856 diagnostics
No memory usage changes detected ✅

@github-actions
Copy link
Contributor

ecosystem-analyzer results

Lint rule Added Removed Changed
possibly-missing-attribute 0 12 0
unresolved-attribute 0 2 0
Total 0 14 0

Full report with detailed diff (timing results)

@sharkdp sharkdp force-pushed the david/fix-1342-attempt-2 branch from b1ea6cb to 964d912 Compare October 13, 2025 12:36
@sharkdp sharkdp force-pushed the david/fix-1342-attempt-2 branch from 964d912 to b221247 Compare October 13, 2025 12:47
@sharkdp sharkdp marked this pull request as ready for review October 13, 2025 12:57
Copy link
Member

Choose a reason for hiding this comment

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

do we really not already have tests like these somewhere...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤷‍♂️

Comment on lines +525 to +535
Note that `variadic_without_annotation` and `variadic_with_annotation` are *not* considered
gradually equivalent to `Callable[..., Any]`, because the latter is not a function-like callable
type:

```py
static_assert(not is_equivalent_to(CallableTypeOf[variadic_without_annotation], Callable[..., Any]))
static_assert(not is_equivalent_to(CallableTypeOf[variadic_with_annotation], Callable[..., Any]))
```

A function with either `*args` or `**kwargs` (and not both) is is not equivalent to a callable with
`...` as the parameter type.
Copy link
Member

Choose a reason for hiding this comment

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

👍

.map(|signature| signature.bind_self(db, Some(self_instance))),
),
false,
true,
Copy link
Member

Choose a reason for hiding this comment

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

strictly speaking, if we're doing it this way rather than with intersections, I suppose we should also have a method_like: bool flag for BoundMethodTypes -- methods not only have all the attributes that functions have, but they also have attributes such as __closure__ that functions do not have

(Not blocking for this PR, though, of course)

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 very much like the Callable[…] & FunctionType / Callable[…] & BoundMethodType approach, but I'd like to move on with this for now, to get to what I'm actually trying to achieve 😄

Copy link
Member

Choose a reason for hiding this comment

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

yeah ikik, that's why I said this is non-blocking 😄

@sharkdp sharkdp merged commit 195e8f0 into main Oct 13, 2025
40 checks passed
@sharkdp sharkdp deleted the david/fix-1342-attempt-2 branch October 13, 2025 13:21
dcreager added a commit that referenced this pull request Oct 13, 2025
…tity

* origin/main: (24 commits)
  Update Python compatibility from 3.13 to 3.14 in README.md (#20852)
  [syntax-errors]: break outside loop F701 (#20556)
  [ty] Treat `Callable`s as bound-method descriptors in special cases (#20802)
  [ty] Do not bind self to non-positional parameters (#20850)
  Fix syntax error false positives on parenthesized context managers (#20846)
  [ty] Remove 'pre-release software' warning (#20817)
  Render unsupported syntax errors in formatter tests (#20777)
  [ty] Treat functions, methods, and dynamic types as function-like `Callable`s (#20842)
  [ty] Move logic for `super()` inference to a new `types::bound_super` submodule (#20840)
  [ty] Fix false-positive diagnostics on `super()` calls (#20814)
  [ty] Move `class_member` to `member` module (#20837)
  [`ruff`] Use DiagnosticTag for more flake8 and numpy rules (#20758)
  [ty] Prefer declared base class attribute over inferred attribute on subclass (#20764)
  [ty] Log files that are slow to type check (#20836)
  Update cargo-bins/cargo-binstall action to v1.15.7 (#20827)
  Update CodSpeedHQ/action action to v4.1.1 (#20828)
  Update Rust crate pyproject-toml to v0.13.7 (#20835)
  Update Rust crate anstream to v0.6.21 (#20829)
  Update Rust crate libc to v0.2.177 (#20832)
  Update Rust crate memchr to v2.7.6 (#20834)
  ...
dcreager added a commit that referenced this pull request Oct 13, 2025
* main: (25 commits)
  [ty] Diagnostic for generic classes that reference typevars in enclosing scope (#20822)
  Update Python compatibility from 3.13 to 3.14 in README.md (#20852)
  [syntax-errors]: break outside loop F701 (#20556)
  [ty] Treat `Callable`s as bound-method descriptors in special cases (#20802)
  [ty] Do not bind self to non-positional parameters (#20850)
  Fix syntax error false positives on parenthesized context managers (#20846)
  [ty] Remove 'pre-release software' warning (#20817)
  Render unsupported syntax errors in formatter tests (#20777)
  [ty] Treat functions, methods, and dynamic types as function-like `Callable`s (#20842)
  [ty] Move logic for `super()` inference to a new `types::bound_super` submodule (#20840)
  [ty] Fix false-positive diagnostics on `super()` calls (#20814)
  [ty] Move `class_member` to `member` module (#20837)
  [`ruff`] Use DiagnosticTag for more flake8 and numpy rules (#20758)
  [ty] Prefer declared base class attribute over inferred attribute on subclass (#20764)
  [ty] Log files that are slow to type check (#20836)
  Update cargo-bins/cargo-binstall action to v1.15.7 (#20827)
  Update CodSpeedHQ/action action to v4.1.1 (#20828)
  Update Rust crate pyproject-toml to v0.13.7 (#20835)
  Update Rust crate anstream to v0.6.21 (#20829)
  Update Rust crate libc to v0.2.177 (#20832)
  ...
dcreager added a commit that referenced this pull request Oct 14, 2025
…rable

* origin/main: (26 commits)
  [ty] Add separate type for typevar "identity" (#20813)
  [ty] Diagnostic for generic classes that reference typevars in enclosing scope (#20822)
  Update Python compatibility from 3.13 to 3.14 in README.md (#20852)
  [syntax-errors]: break outside loop F701 (#20556)
  [ty] Treat `Callable`s as bound-method descriptors in special cases (#20802)
  [ty] Do not bind self to non-positional parameters (#20850)
  Fix syntax error false positives on parenthesized context managers (#20846)
  [ty] Remove 'pre-release software' warning (#20817)
  Render unsupported syntax errors in formatter tests (#20777)
  [ty] Treat functions, methods, and dynamic types as function-like `Callable`s (#20842)
  [ty] Move logic for `super()` inference to a new `types::bound_super` submodule (#20840)
  [ty] Fix false-positive diagnostics on `super()` calls (#20814)
  [ty] Move `class_member` to `member` module (#20837)
  [`ruff`] Use DiagnosticTag for more flake8 and numpy rules (#20758)
  [ty] Prefer declared base class attribute over inferred attribute on subclass (#20764)
  [ty] Log files that are slow to type check (#20836)
  Update cargo-bins/cargo-binstall action to v1.15.7 (#20827)
  Update CodSpeedHQ/action action to v4.1.1 (#20828)
  Update Rust crate pyproject-toml to v0.13.7 (#20835)
  Update Rust crate anstream to v0.6.21 (#20829)
  ...
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.

Literal promotion for callables results in missing attributes Type relations for function-like callables are too strict

3 participants