Skip to content

Conversation

@lipefree
Copy link
Contributor

@lipefree lipefree commented Jun 2, 2025

Summary

Fixes astral-sh/ty#567.

Since lambda functions are instance of types.FunctionType, we should be able to access to attribute such as __code__, __name__ and more. With this PR, we will have the following type and no more errors :

from typing import reveal_type

x = lambda y: y

reveal_type(x.__code__)  # revealed: CodeType

Test Plan

I have added mdtests, but since I am not familiar with attributes of types.FunctionType, my tests may be either too redundant or not exhaustive enough.

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Jun 2, 2025
@AlexWaygood AlexWaygood changed the title [ty] Treat lambda functions as types.FunctionType [ty] Treat lambda functions as instances of types.FunctionType Jun 2, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Jun 2, 2025

mypy_primer results

Changes were detected when running on open source projects
static-frame (https://github.com/static-frame/static-frame)
- error[unresolved-attribute] static_frame/core/util.py:571:5: Unresolved attribute `__name__` on type `(rhs, lhs, func=Any) -> Unknown`.
- Found 1944 diagnostics
+ Found 1943 diagnostics

bokeh (https://github.com/bokeh/bokeh)
+ error[invalid-assignment] src/bokeh/core/templates.py:112:1: Object of type `dict[str, () -> Unknown]` is not assignable to `dict[str, () -> Template]`
- Found 951 diagnostics
+ Found 952 diagnostics

sphinx (https://github.com/sphinx-doc/sphinx)
- error[unresolved-attribute] sphinx/ext/autodoc/preserve_defaults.py:28:16: Type `() -> Unknown` has no attribute `__name__`
- Found 657 diagnostics
+ Found 656 diagnostics

dd-trace-py (https://github.com/DataDog/dd-trace-py)
- error[unresolved-attribute] ddtrace/debugging/_signal/utils.py:227:38: Type `((Any, /) -> bool) | ((_) -> Unknown)` has no attribute `__name__`
- error[unresolved-attribute] ddtrace/debugging/_signal/utils.py:257:38: Type `((Any, /) -> bool) | ((_) -> Unknown)` has no attribute `__name__`
- error[unresolved-attribute] ddtrace/debugging/_signal/utils.py:313:41: Type `((Any, /) -> bool) | ((_) -> Unknown)` has no attribute `__name__`
- error[unresolved-attribute] ddtrace/debugging/_signal/utils.py:332:34: Type `((Any, /) -> bool) | ((_) -> Unknown)` has no attribute `__name__`
- error[unresolved-attribute] ddtrace/debugging/_signal/utils.py:358:37: Type `((Any, /) -> bool) | ((_) -> Unknown)` has no attribute `__name__`
+ warning[possibly-unbound-attribute] ddtrace/debugging/_signal/utils.py:227:38: Attribute `__name__` on type `((Any, /) -> bool) | ((_) -> Unknown)` is possibly unbound
+ warning[possibly-unbound-attribute] ddtrace/debugging/_signal/utils.py:257:38: Attribute `__name__` on type `((Any, /) -> bool) | ((_) -> Unknown)` is possibly unbound
+ warning[possibly-unbound-attribute] ddtrace/debugging/_signal/utils.py:313:41: Attribute `__name__` on type `((Any, /) -> bool) | ((_) -> Unknown)` is possibly unbound
+ warning[possibly-unbound-attribute] ddtrace/debugging/_signal/utils.py:332:34: Attribute `__name__` on type `((Any, /) -> bool) | ((_) -> Unknown)` is possibly unbound
+ warning[possibly-unbound-attribute] ddtrace/debugging/_signal/utils.py:358:37: Attribute `__name__` on type `((Any, /) -> bool) | ((_) -> Unknown)` is possibly unbound
- error[unresolved-attribute] tests/tracer/test_writer.py:311:34: Type `(*args) -> Unknown` has no attribute `__get__`
- Found 6861 diagnostics
+ Found 6860 diagnostics

sympy (https://github.com/sympy/sympy)
- error[unsupported-operator] sympy/codegen/tests/test_applications.py:23:17: Operator `<` is not supported for types `Variable` and `Variable`
- error[unresolved-attribute] sympy/tensor/array/array_comprehension.py:342:58: Type `() -> Unknown` has no attribute `__name__`
- Found 18575 diagnostics
+ Found 18573 diagnostics

@AlexWaygood
Copy link
Member

+ error[invalid-assignment] src/bokeh/core/templates.py:112:1: Object of type `dict[str, () -> Unknown]` is not assignable to `dict[str, () -> Template]`

This error is occuring because dict is invariant: https://github.com/bokeh/bokeh/blob/7d6cedea470cb68f25afd126a29a45370c08f443/src/bokeh/core/templates.py#L112-L127. Bidirectional inference would fix this.

+ warning[possibly-unbound-attribute] ddtrace/debugging/_signal/utils.py:227:38: Attribute `__name__` on type `((Any, /) -> bool) | ((_) -> Unknown)` is possibly unbound
+ warning[possibly-unbound-attribute] ddtrace/debugging/_signal/utils.py:257:38: Attribute `__name__` on type `((Any, /) -> bool) | ((_) -> Unknown)` is possibly unbound
+ warning[possibly-unbound-attribute] ddtrace/debugging/_signal/utils.py:313:41: Attribute `__name__` on type `((Any, /) -> bool) | ((_) -> Unknown)` is possibly unbound
+ warning[possibly-unbound-attribute] ddtrace/debugging/_signal/utils.py:332:34: Attribute `__name__` on type `((Any, /) -> bool) | ((_) -> Unknown)` is possibly unbound
+ warning[possibly-unbound-attribute] ddtrace/debugging/_signal/utils.py:358:37: Attribute `__name__` on type `((Any, /) -> bool) | ((_) -> Unknown)` is possibly unbound

These are because we do not infer Callable types created from Callable type expressions as being function-like. Whether or not we should do that is a complicated question that's discussed in other issues.

So all the primer hits here look good to me!

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.

Thank you!

@AlexWaygood AlexWaygood merged commit f379eb6 into astral-sh:main Jun 2, 2025
35 checks passed
dcreager added a commit that referenced this pull request Jun 2, 2025
…aration

* origin/main:
  [ty] Treat lambda functions as instances of types.FunctionType (#18431)
  [ty] Fix false positives for legacy `ParamSpec`s inside `Callable` type expressions (#18426)
  [ty] Improve diagnostics if the user attempts to import a stdlib module that does not exist on their configured Python version (#18403)
  Update taiki-e/install-action action to v2.52.4 (#18420)
  Update docker/build-push-action action to v6.18.0 (#18422)
  [ty] Fix server hang after shutdown request (#18414)
  Update Rust crate libcst to v1.8.0 (#18424)
  Update Rust crate clap to v4.5.39 (#18419)
  Update cargo-bins/cargo-binstall action to v1.12.6 (#18416)
  Update dependency mdformat-mkdocs to v4.3.0 (#18421)
  Update pre-commit dependencies (#18418)
  Update dependency ruff to v0.11.12 (#18417)
  [ty] Ensure `Literal` types are considered assignable to anything their `Instance` supertypes are assignable to (#18351)
  [ty] Promote projects to good that now no longer hang (#18370)
  Sync vendored typeshed stubs (#18407)
  [ty] Fix multithreading related hangs and panics (#18238)
  Support relative `--ty-path` in ty-benchmark (#18385)
  [ty] Update docs for Python version inference (#18397)
  [ty] Infer the Python version from the environment if feasible (#18057)
  Implement template strings (#17851)
@dhruvmanila dhruvmanila added the bug Something isn't working label Jun 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Attributes of types.FunctionType should be available on lambda functions

3 participants