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

Generic serializer #377

Merged
merged 2 commits into from
Apr 16, 2024
Merged

Generic serializer #377

merged 2 commits into from
Apr 16, 2024

Conversation

Daverball
Copy link
Contributor

@Daverball Daverball commented Apr 14, 2024

Supersedes: #376

In the end I've had to give up on making a gradual signature for dumps work, because the @property hack doesn't interact well with @staticmethod/@classmethod. We don't really get that much out of it anyways, so instead we now have fallback overloads when the Protocol doesn't match and a default type for the TypeVar. In most cases the Protocol should still match though, even with additional parameters, it matches json pickle and _CompactJSON in both mypy and pyright and infers the correct type for the Serializer when passed as the serializer argument, both the positional and keyword version of the overloads were tested.

@Daverball Daverball mentioned this pull request Apr 14, 2024
@Daverball Daverball force-pushed the generic-serializer branch 3 times, most recently from 16f1d9f to a65bf26 Compare April 14, 2024 13:13
@davidism davidism added this to the 2.2.0 milestone Apr 16, 2024
@davidism
Copy link
Member

Thanks, pushed to update the comments and add comments about each overload. I'm confused why a separate overload is required for positional vs keyword argument use, but it does fail if I remove the keyword version.

It's pretty silly that number of lines in serializer.py doubled just to support this one argument. I'd really like to see that improved in the spec/tools.

Could you link to any relevant mypy and pyright issues, already existing or that you open? I don't know what I'd be searching for or reporting myself.

@davidism davidism merged commit 135eb23 into pallets:main Apr 16, 2024
11 checks passed
@Daverball
Copy link
Contributor Author

@davidism Yes, having redundant overloads just because of syntax restrictions is one of my pet peeves of Python typing. I did start a discussion on the Discourse a while back, but that was before the typing governance process and the typing council was established, so the discussion was quickly deferred.

I know this has come up in various discussions in the past and there are probably even more recent topics in the Discourse that cover it, but so far nothing has really happened, since how overload resolution is supposed to work isn't even part of the typing spec yet. So that pretty much needs to happen before we can improve things.

The topic contains some links to at least one more related issue or discussion
https://discuss.python.org/t/some-improvements-to-function-overloads/36275

@Daverball Daverball deleted the generic-serializer branch April 16, 2024 17:21
@Daverball
Copy link
Contributor Author

Oh and I reported the issue we were seeing with --extra-checks in mypy here: python/mypy#17122

Although we'd probably need ReadOnly to work within Protocol before that's useful, since @property on Protocol only works for methods and attributes, not for @classmethod and @staticmethod. That's another one of my pet peeves, that we use @property as a crutch for read only attributes, when ReadOnly is more flexible and compatible with more things, so more in the spirit of duck typing.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants