pydantic / pydantic-extra-types

Extra Pydantic types.
MIT License
176 stars 47 forks source link

mypy is not happy with `ulid.ULID` #152

Closed sinyo-matu closed 5 months ago

sinyo-matu commented 6 months ago

Description

when using pydantic-extra-types.ulid.ULID like below

# model.py
from pydantic import BaseModel
from pydantic_extra_types.ulid import ULID

class Something(BaseModel):
    id: ULID
# application.py
from ulid import ULID

def code_use_something_id(id:ULID) -> None:
    print(id)
# test_application.py
from pydantic_ulid_mypy.application import code_use_something_id
from pydantic_ulid_mypy.models import Something

def test_application():
    data = {
        "id": "01BTGNYV6HRNK8K8VKZASZCFPE"
    }
    something = Something.model_validate(data)
    code_use_something_id(something.id)

runnig mypy will outputs

tests/test_application.py:10: error: Argument 1 to "code_use_something_id" has incompatible type >"pydantic_extra_types.ulid.ULID"; expected "ulid.ULID" [arg-type]

this forces user use mypy to use pydantic_extra_types.ulid.ULID in their type annotations or manually cast pydantic_extra_types.ulid.ULID to ulid.ULID which is not so user friendly IMO

Example Code

https://github.com/sinyo-matu/pydantic-ulid-mypy

Solution(IMO)

I think we can make ULID subclass of python-ulid.ULID instead of subclassing _repr.Representation(which I have no idea why it's nesessary)

Can this be a solution? https://github.com/sinyo-matu/pydantic-extra-types/blob/0e5f5507e6470766e2472fe65c89d5bba632ad40/pydantic_extra_types/ulid.py#L26

Python, Pydantic & OS Version

Python: 3.12 Pydantic: 2.6.3 Pydantic-extra-types: 2.6.0 OS Version:

System Version: macOS 14.2.1 (23C71) Kernel Version: Darwin 23.2.0 Boot Volume: Macintosh HD

mdomke commented 5 months ago

The ulid-library in version 2.3.0 does now natively support Pydantic so that the wrapper type is not necessary anymore. See here

sinyo-matu commented 5 months ago

Thank you for informing that. Got it.