False bad-assignment when __new__ returns a bare type variable solved from the assignment context
Summary
When a class declares __new__ whose return annotation is a bare type variable, Pyrefly does not solve that type variable from the expected (declared) type of the assignment. It falls back to the class instance type and reports bad-assignment, even though reveal_type on the very same variable reports the correctly solved type.
The same expected-type inference works for a plain function, for a classmethod factory, and even for __new__ returning list[T] — only the bare -> T case fails, which is what makes this look like a bug rather than an intentional restriction.
Pyright/basedpyright accept all of these.
Repro
from typing import TYPE_CHECKING, reveal_type
def make[T]() -> T: ...
class Factory:
@classmethod
def create[T](cls) -> T: ...
class ViaNewList:
def __new__[T](cls) -> list[T]: ...
class ViaNew:
def __new__[T](cls) -> T: ...
class ViaNewGuarded:
if TYPE_CHECKING:
def __new__[T](cls) -> T: ...
a: int = make() # ok
b: int = Factory.create() # ok
c: list[int] = ViaNewList() # ok
d: int = ViaNew() # false positive
e: int = ViaNewGuarded() # false positive
reveal_type(d)
reveal_type(e)
Actual
$ pyrefly check --python-version 3.14 repro.py
ERROR `ViaNew` is not assignable to `int` [bad-assignment]
ERROR `ViaNewGuarded` is not assignable to `int` [bad-assignment]
INFO revealed type: int [reveal-type]
INFO revealed type: int [reveal-type]
INFO 2 errors
Note the inconsistency: the assignment is rejected, while reveal_type of the same variable reports int — so the declared type is resolved correctly and only the assignability check uses the class instance type. Whether the __new__ is declared under if TYPE_CHECKING makes no difference.
Expected
No errors, i.e. the same result as for make(), Factory.create() and ViaNewList(): the type variable is solved from the expected type of the assignment.
Other type checkers
$ basedpyright repro.py
0 errors, 4 warnings # warnings are reportInvalidTypeVarUse on the four declarations
# Type of "d" is "int", Type of "e" is "int"
Mypy infers int for both declarations as well (no [assignment] error); it does report a separate diagnostic on the definitions — "__new__" must return a class instance (got "T") — so it evidently treats this pattern as unsupported by design, while still using the return type at the call site.
Why it matters
This is the standard way a library declares a field/descriptor factory so that the factory call can be assigned to an annotated attribute:
class Field:
if TYPE_CHECKING:
def __new__[T](cls, *args, **kwargs) -> T: ...
class Model:
name: str = Field()
count: int | None = Field()
Under Pyrefly every such declaration in user code is reported, so in a model-heavy codebase the error count scales with the number of declared fields. Giving the type variable a default ([T = Any]) silences it, but that also disables the checking the annotation is there for, so it is not a usable workaround for a library that wants wrong annotations reported.
Versions
- pyrefly 1.2.0 and 1.3.0-dev.4 — identical behaviour in both
- for comparison: basedpyright 1.39.10 (pyright 1.1.412), mypy 2.3.1
- Python 3.14.0
Thanks for the detailed report! This no longer reproduces on the latest release (1.4.1): both assignments are accepted and d and e reveal int, as this sandbox shows. Could you check on 1.4.1 and let us know? We'll close this once you confirm.
Thanks for checking — and you're right for the setup you tried. It turns out the behaviour depends on which config file Pyrefly uses, which my original report didn't pin down. Sorry about that.
With a native pyrefly.toml the repro is accepted, as you say. It still reproduces when Pyrefly imports settings from a pyrightconfig.json. Same file, same binary, only the config differs.
The only change to the repro from the issue body is one extra line at the end, as a sanity check that the file is really being analysed:
sanity: int = "must be reported"
$ ls
pyrightconfig.json repro.py
$ cat pyrightconfig.json
{"pythonVersion": "3.14"}
$ pyrefly check repro.py
ERROR `ViaNew` is not assignable to `int` [bad-assignment]
ERROR `ViaNewGuarded` is not assignable to `int` [bad-assignment]
INFO revealed type: int [reveal-type]
INFO revealed type: int [reveal-type]
ERROR `Literal['must be reported']` is not assignable to `int` [bad-assignment]
INFO 3 errors
No `pyrefly.toml` found — using settings imported from your `pyrightconfig.json` (preset: default).
$ rm pyrightconfig.json && printf 'python-version = "3.14"\n' > pyrefly.toml
$ pyrefly check repro.py
INFO revealed type: int [reveal-type]
INFO revealed type: int [reveal-type]
ERROR `Literal['must be reported']` is not assignable to `int` [bad-assignment]
INFO 1 error
The contents of the pyrightconfig.json don't matter — {}, {"pythonVersion": "3.14"} and {"typeCheckingMode": "basic" | "standard" | "strict"} all produce the two errors, while preset = "default" in a pyrefly.toml does not. So it looks specific to the pyright-compat path rather than to a preset or a severity setting.
Measured on 1.2.0, 1.4.0, 1.4.1, 1.5.0.dev1 and 1.5.0 (macOS arm64). The native path accepts the code in every one of those versions, including 1.2.0, so nothing regressed and nothing was fixed here — my original report was simply measured in a directory that happened to contain a pyrightconfig.json, which is my mistake. In all modes reveal_type reports the correctly solved int, so only the assignability check differs.
Two notes in case they're useful:
- With no config file at all,
pyrefly check repro.pyin that directory reports0 errors— the obvioussanity: int = "must be reported"isn't reported either, with the footerNo pyrefly.toml found — using preset basic.Worth keeping in mind when verifying this kind of report. - Is the difference between the two config paths intended? If the pyright-compat path is meant to match Pyright here, then Pyright accepts this code, so the remaining difference looks unintended. Happy to open a separate issue for just that and let this one be closed, if you prefer.
Thanks for digging into this! You're right. Importing a pyrightconfig.json turns off first-use inference, and that's what triggers the error. Pyright accepts this code, so we'll treat it as a bug and keep this issue open. There's no need to file a new one.
Here's the example in the sandbox:
https://pyrefly.org/sandbox/?project=v2.jZRBCoNADEWvEnSji_EAll6hK-mmU2TUqAEbZTJipZcvY6vQQqUHyCd5P_8_flRmCFmLMMyWmtapsueatu4UdALENVo1kWtVTVacGsXfUZtOECLBZXijEydr-s5kTjh9hI9xyvNLdo3KTpaPyVJIksRbrblKgdjB8T0YxQAhFGOTQmEqZUSo4RuyA4svrNCixcO6Onh2g9_XaRbD5OZVUAeT7bnRgVfE--Cz9CW6cP7f5T0mmnd9eAI