facebook / facebook/pyrefly

Pydantic union types disable Pyrefly assignment checks

Open
#4,131 3 comments 0 reactions 1 assignee Claimed by @asukaminato0721 View on GitHub
help wanted pydantic typechecking
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

When annotating a field in a Pydantic model with a union type, Pyrefly no longer checks the types of values you assign to that field.

Reproducer:

```py
from pydantic import BaseModel

# Outside a Pydantic model, without a union: this fails
x: str = 5

# Outside a Pydantic model, with a union: this fails
y: str | bool = 5

class MyModel(BaseModel):
field: str

class MyUnionModel(BaseModel):
field: str | bool

# Inside a model, without a union: this fails
MyModel(field=5)

# Inside a model, with a union: this passes (crashes at runtime!)
MyUnionModel(field=5)
```

Pyrefly output is the following:

```
ERROR `Literal[5]` is not assignable to `str` [bad-assignment]
--> repro.py:5:10
|
5 | x: str = 5
| --- ^
| |
| declared type
|
ERROR `Literal[5]` is not assignable to `bool | str` [bad-assignment]
--> repro.py:8:17
|
8 | y: str | bool = 5
| ---------- ^
| |
| declared type
|
ERROR Argument `Literal[5]` is not assignable to parameter `field` with type `bytearray | bytes | str` in function `MyModel.__init__` [bad-argument-type]
--> repro.py:20:15
|
21 | MyModel(field=5)
| ^
|
INFO 3 errors
```

Tested with both latest stable (`1.1.1`) and latest dev (`1.2.0-dev.2` at the time of writing).

### Sandbox Link

_No response_

### (Only applicable for extension issues) IDE Information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.