ruff/crates/ty_python_semantic/resources/mdtest/call/subclass_of.md
InSync 46be305ad2
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
[ty] Include synthesized arguments in displayed counts for too-many-positional-arguments (#18098)
## Summary

Resolves [#290](https://github.com/astral-sh/ty/issues/290).

All arguments, synthesized or not, are now accounted for in
`too-many-positional-arguments`'s error message.

For example, consider this example:

```python
class C:
	def foo(self): ...

C().foo(1)  # !!!
```

Previously, ty would say:

> Too many positional arguments to bound method foo: expected 0, got 1

After this change, it will say:

> Too many positional arguments to bound method foo: expected 1, got 2

This is what Python itself does too:

```text
Traceback (most recent call last):
  File "<python-input-0>", line 3, in <module>
    C().foo()
    ~~~~~~~^^
TypeError: C.foo() takes 0 positional arguments but 1 was given
```

## Test Plan

Markdown tests.
2025-05-14 22:51:23 -04:00

1.4 KiB

Call type[...]

Single class

Trivial constructor

class C: ...

def _(subclass_of_c: type[C]):
    reveal_type(subclass_of_c())  # revealed: C

Non-trivial constructor

class C:
    def __init__(self, x: int): ...

def _(subclass_of_c: type[C]):
    reveal_type(subclass_of_c(1))  # revealed: C

    # error: [invalid-argument-type] "Argument to bound method `__init__` is incorrect: Expected `int`, found `Literal["a"]`"
    reveal_type(subclass_of_c("a"))  # revealed: C
    # error: [missing-argument] "No argument provided for required parameter `x` of bound method `__init__`"
    reveal_type(subclass_of_c())  # revealed: C
    # error: [too-many-positional-arguments] "Too many positional arguments to bound method `__init__`: expected 2, got 3"
    reveal_type(subclass_of_c(1, 2))  # revealed: C

Dynamic base

from typing import Any
from ty_extensions import Unknown

def _(subclass_of_any: type[Any], subclass_of_unknown: type[Unknown]):
    reveal_type(subclass_of_any())  # revealed: Any
    reveal_type(subclass_of_any("any", "args", 1, 2))  # revealed: Any
    reveal_type(subclass_of_unknown())  # revealed: Unknown
    reveal_type(subclass_of_unknown("any", "args", 1, 2))  # revealed: Unknown

Unions of classes

class A: ...
class B: ...

def _(subclass_of_ab: type[A | B]):
    reveal_type(subclass_of_ab())  # revealed: A | B