Commit Graph
8 Commits
Author SHA1 Message Date
Micha Reiser 230f4052e3 [ty] Run the SQLModel mdtest on Python 3.13 (#29906) 2026-11-02 07:01:30 -08:00
David Peter eac76aac4a [ty] Pydantic: Support custom __init__ methods (#26699)
## Summary

A Pydantic model with a base class with a custom `__init__` method
previously prevented us from adding `extra: **Unknown` to the models
constructor.

## Test Plan

Added Markdown tests
2026-07-10 06:06:53 -07:00
David Peter f619cb9b25 [ty] Distinguish lax and strict mode for Pydantic models (#26587)
## Summary

By default, Pydantic models can convert input values. This is called
["lax
mode"](https://pydantic.dev/docs/validation/latest/concepts/strict_mode/).
For example:

```py
class Model(BaseModel):
    values: list[int]

Model(values=[1, 2, 3])             # okay
Model(values=["1", "2", "3"])       # also okay
Model(values=set(["1", "2", "3"]))  # also okay

Model(values=[None])    # error
Model(values=[[1, 2]])  # error
```

Allowed conversions are listed in [this
table](https://pydantic.dev/docs/validation/latest/concepts/conversion_table)
in Pydantic's documentation. To support this, I ended up converging to a
solution that is very similar to what Pyrefly does (according to its
documentation, I did not consult the implementation). We add a few type
aliases like `type LaxInt = int | bytes | str | float | Decimal` to
`ty_extensions.pydantic`, and use those to eagerly transform the input
types. For example, we map `list[int] -> Iterable[LaxInt]` (see mdtests
for why we use `Iterable`). I did consider several alternatives though:

- A very permissive mode in which we accept `Any` input type in lax
mode. I considered this to be a viable alternative since the benefit of
modeling this more precisely seems relatively small. Most Pydantic
models will be constructed from data that enters through an I/O
boundary, not via an explicit `Model(values=[1, 2, 3])` call. So it
doesn't seem very likely that we'll catch a lot of errors with this. I
ended up rejecting this idea because I didn't like the impact on our
LSP: instead of showing useful constructor signatures, we would end up
showing `Any` types everywhere.
- Instead of introducing various `IntLax`, `IntStr`, ... type aliases, I
started implementing a version where we would have a `Lax[..]` special
form. Input types would be converted by simply wrapping the annotated
type in `Lax[..]`, e.g. `list[int] -> Lax[list[int]]`. The problem with
this is that we would need to apply this type mapping lazily (since we
would like to preserve `Lax[list[int]]` in the annotation). This would
require an entirely new `Type` enum variant, and would require us to
implement the conversion table inside our type relations. This seemed
too complex / invasive.

## Test Plan

Updated and added Markdown tests, verified every single test against
Pydantic's runtime behavior.
2026-07-07 16:27:08 +00:00
2f839db9e4 [ty] respect __new__ and metaclass __call__ return types (#24357)
Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
Co-authored-by: Denys Zhak <denyszhak@gmail.com>
2026-04-03 09:10:34 -07:00
4898ae9d9a [ty] Recognize __dataclass_transform__ to support SQLModel (#23070)
## Summary

For backwards compatibility with pre-3.11 Python, ty now recognizes any
function named `__dataclass_transform__` as equivalent to
`typing.dataclass_transform`, regardless of which module it is defined
in.

This matches pyright's behavior and enables proper type checking for
libraries like SQLModel that use this pattern to support older Python
versions. The name matches the attribute set at runtime by the
decorator.

Reference:
https://typing.python.org/en/latest/spec/dataclasses.html#runtime-behavior

closes https://github.com/astral-sh/ty/issues/1329

## Test plan

- Added test for `__dataclass_transform__` recognition in
`dataclass_transform.md`
- Updated `sqlmodel.md` external tests to verify the fix works with real
SQLModel patterns

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: David Peter <mail@david-peter.de>
2026-02-05 08:39:54 +00:00
David Peter 0bac023cd2 [ty] Lockfiles for mdtests with external dependencies (#22077)
## Summary

Add lockfiles for all mdtests which make use of external dependencies.
When running tests normally, we use this lockfile when creating the
temporary venv using `uv sync --locked`. A new
`MDTEST_UPGRADE_LOCKFILES` environment variable is used to switch to a
mode in which those lockfiles can be updated or regenerated. When using
the Python mdtest runner, this environment variable is automatically set
(because we use this command while developing, not to simulate exactly
what happens in CI). A command-line flag is provided to opt out of this.

## Test Plan

### Using the mdtest runner

#### Adding a new test (no lockfile yet)

* Removed `attrs.lock` to simulate this
* Ran `uv run crates/ty_python_semantic/mdtest.py -e external/`. The
lockfile is generated and the test succeeds.

#### Upgrading/downgrading a dependency

* Changed pydantic requirement from `pydantic==2.12.2` to
`pydantic==2.12.5` (also tested with `2.12.0`)
* Ran `uv run crates/ty_python_semantic/mdtest.py -e external/`. The
lockfile is updated and the test succeeds.

### Using cargo

#### Adding a new test (no lockfile yet)

* Removed `attrs.lock` to simulate this
* Ran `MDTEST_EXTERNAL=1 cargo test -p ty_python_semantic --test mdtest
mdtest__external` "naively", which outputs:
> Failed to setup in-memory virtual environment with dependencies:
Lockfile not found at
'/home/shark/ruff/crates/ty_python_semantic/resources/mdtest/external/attrs.lock'.
Run with `MDTEST_UPGRADE_LOCKFILES=1` to generate it.
* Ran `MDTEST_UPGRADE_LOCKFILES=1 MDTEST_EXTERNAL=1 cargo test -p
ty_python_semantic --test mdtest mdtest__external`. The lockfile is
updated and the test succeeds.

#### Upgrading/downgrading a dependency

* Changed pydantic requirement from `pydantic==2.12.2` to
`pydantic==2.12.5` (also tested with `2.12.0`)
* Ran `MDTEST_EXTERNAL=1 cargo test -p ty_python_semantic --test mdtest
mdtest__external` "naively", which outputs a similar error message as
above.
* Ran the command suggested in the error message (`MDTEST_EXTERNAL=1
MDTEST_UPGRADE_LOCKFILES=1 cargo test -p ty_python_semantic --test
mdtest mdtest__external`). The lockfile is updated and the test
succeeds.
2025-12-19 14:29:52 +01:00
David Peter a9be810c38 [ty] Type inference for @asynccontextmanager (#21876)
## Summary

This PR adds special handling for `asynccontextmanager` calls as a
temporary solution for https://github.com/astral-sh/ty/issues/1804. We
will be able to remove this soon once we have support for generic
protocols in the solver.

closes https://github.com/astral-sh/ty/issues/1804

## Ecosystem

```diff
+ tests/test_downloadermiddleware.py:305:56: error[invalid-argument-type] Argument to bound method `download` is incorrect: Expected `Spider`, found `Unknown | Spider | None`
+ tests/test_downloadermiddleware.py:305:56: warning[possibly-missing-attribute] Attribute `spider` may be missing on object of type `Crawler | None`
```
These look like true positives

```diff
+ pymongo/asynchronous/database.py:1021:35: error[invalid-assignment] Object of type `(AsyncClientSession & ~AlwaysTruthy & ~AlwaysFalsy) | (_ServerMode & ~AlwaysFalsy) | Unknown | Primary` is not assignable to `_ServerMode | None`
+ pymongo/asynchronous/database.py:1025:17: error[invalid-argument-type] Argument to bound method `_conn_for_reads` is incorrect: Expected `_ServerMode`, found `_ServerMode | None`
```

Known problems or true positives, just caused by the new type for
`session`

```diff
- src/integrations/prefect-sqlalchemy/prefect_sqlalchemy/database.py:269:16: error[invalid-return-type] Return type does not match returned value: expected `Connection | AsyncConnection`, found `_GeneratorContextManager[Unknown, None, None] | _AsyncGeneratorContextManager[Unknown, None] | Connection | AsyncConnection`
+ src/integrations/prefect-sqlalchemy/prefect_sqlalchemy/database.py:269:16: error[invalid-return-type] Return type does not match returned value: expected `Connection | AsyncConnection`, found `_GeneratorContextManager[Unknown, None, None] | _AsyncGeneratorContextManager[AsyncConnection, None] | Connection | AsyncConnection`
```

Just a more concrete type

```diff
- src/prefect/flow_engine.py:1277:24: error[missing-argument] No argument provided for required parameter `cls`
- src/prefect/server/api/server.py:696:49: error[missing-argument] No argument provided for required parameter `cls`
- src/prefect/task_engine.py:1426:24: error[missing-argument] No argument provided for required parameter `cls`
```

Good

## Test Plan

* Adapted and newly added Markdown tests
* Tested on internal codebase
2025-12-09 22:49:00 +01:00
David Peter dfd6ed0524 [ty] mdtests with external dependencies (#20904)
## Summary

This PR adds the possibility to write mdtests that specify external
dependencies in a `project` section of TOML blocks. For example, here is
a test that makes sure that we understand Pydantic's dataclass-transform
setup:

````markdown
```toml
[environment]
python-version = "3.12"
python-platform = "linux"

[project]
dependencies = ["pydantic==2.12.2"]
```

```py
from pydantic import BaseModel

class User(BaseModel):
    id: int
    name: str

user = User(id=1, name="Alice")
reveal_type(user.id)  # revealed: int
reveal_type(user.name)  # revealed: str

# error: [missing-argument] "No argument provided for required parameter
`name`"
invalid_user = User(id=2)
```
````

## How?

Using the `python-version` and the `dependencies` fields from the
Markdown section, we generate a `pyproject.toml` file, write it to a
temporary directory, and use `uv sync` to install the dependencies into
a virtual environment. We then copy the Python source files from that
venv's `site-packages` folder to a corresponding directory structure in
the in-memory filesystem. Finally, we configure the search paths
accordingly, and run the mdtest as usual.

I fully understand that there are valid concerns here:
* Doesn't this require network access? (yes, it does)
* Is this fast enough? (`uv` caching makes this almost unnoticeable,
actually)
* Is this deterministic? ~~(probably not, package resolution can depend
on the platform you're on)~~ (yes, hopefully)

For this reason, this first version is opt-in, locally. ~~We don't even
run these tests in CI (even though they worked fine in a previous
iteration of this PR).~~ You need to set `MDTEST_EXTERNAL=1`, or use the
new `-e/--enable-external` command line option of the `mdtest.py`
runner. For example:
```bash
# Skip mdtests with external dependencies (default):
uv run crates/ty_python_semantic/mdtest.py

# Run all mdtests, including those with external dependencies:
uv run crates/ty_python_semantic/mdtest.py -e

# Only run the `pydantic` tests. Use `-e` to make sure it is not skipped:
uv run crates/ty_python_semantic/mdtest.py -e pydantic
```

## Why?

I believe that this can be a useful addition to our testing strategy,
which lies somewhere between ecosystem tests and normal mdtests.
Ecosystem tests cover much more code, but they have the disadvantage
that we only see second- or third-order effects via diagnostic diffs. If
we unexpectedly gain or lose type coverage somewhere, we might not even
notice (assuming the gradual guarantee holds, and ecosystem code is
mostly correct). Another disadvantage of ecosystem checks is that they
only test checked-in code that is usually correct. However, we also want
to test what happens on wrong code, like the code that is momentarily
written in an editor, before fixing it. On the other end of the spectrum
we have normal mdtests, which have the disadvantage that they do not
reflect the reality of complex real-world code. We experience this
whenever we're surprised by an ecosystem report on a PR.

That said, these tests should not be seen as a replacement for either of
these things. For example, we should still strive to write detailed
self-contained mdtests for user-reported issues. But we might use this
new layer for regression tests, or simply as a debugging tool. It can
also serve as a tool to document our support for popular third-party
libraries.

## Test Plan

* I've been locally using this for a couple of weeks now.
* `uv run crates/ty_python_semantic/mdtest.py -e`
2025-12-08 11:44:20 +01:00