Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Positional-only arguments #705

Open
einarwar opened this issue Jun 19, 2024 · 0 comments
Open

Positional-only arguments #705

einarwar opened this issue Jun 19, 2024 · 0 comments

Comments

@einarwar
Copy link

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:

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

with:

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

This might also apply to other methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant