jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.01k stars 59 forks source link

Positional-only arguments #705

Open einarwar opened 2 weeks ago

einarwar commented 2 weeks ago

Description

Using msgspec.json.Decoder.decode() with a keyword arguments results in an error. Im guessing this has to do with the C-implementation. However, the error is only caught in runtime. Take the following example; Calling the function without a keyword works fine, calling it with does not.

import msgspec

class Foo(msgspec.Struct):
    a: int

decoder = msgspec.json.Decoder(Foo)

foo = Foo(a=1)
encoded = msgspec.json.encode(foo)

decoded = decoder.decode(encoded) # Works
decoded = decoder.decode(data=encoded) # Raises TypeError

I think this could be fixed by modifying the stub and adding a positional-only delimiter. Then one would get a warning if a type-checker like mypy or pyright is used.

By replacing: https://github.com/jcrist/msgspec/blob/2c37da090b2c5fcb2ecca9ae00274c67fabb85cf/msgspec/json.pyi#L76 with:

def decode(self, data: Union[bytes, str], /) -> T: ... 

This might also apply to other methods