Skip to content

Improve typing of run by using @overload#685

Merged
gabrieldemarmiesse merged 2 commits intogabrieldemarmiesse:masterfrom
patrick91:feature/overlods
Oct 13, 2025
Merged

Improve typing of run by using @overload#685
gabrieldemarmiesse merged 2 commits intogabrieldemarmiesse:masterfrom
patrick91:feature/overlods

Conversation

@patrick91
Copy link
Contributor

@patrick91 patrick91 commented Sep 26, 2025

Closes #580

Credits go to @Dreamsorcerer

I didn't think of this first, but mixing the typing.Unpack approach with the default signature works around the missing default values in the docs:

CleanShot 2025-09-26 at 22 03 38@2x

From what I understood there's no way of using typing.Unpack and passing default values, but in this case we still get some benefits from using them since we'd need to duplicate a lot of args (I might do something similar in one of my projects :D )


import pydantic
from typing_extensions import TypeAlias
from typing_extensions import TypeAlias, Unpack
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the original PR this was conditionally imported from typing if the Python version was new enough, but typing_extension does the same already 😊

Copy link

@Dreamsorcerer Dreamsorcerer Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea of the version check was to be able to remove typing-extensions as a dependency in versions of Python that don't need it (e.g. https://github.com/aio-libs/multidict/blob/18344e20b7a0c883b48de685174a78b8b33663e7/setup.cfg#L45), plus allow tools like pyupgrade/ruff to automatically remove the old imports when the project stops supporting a particular version of Python.

but typing_extension does the same already

Obviously typing-extensions will have all of these things already, given that's it's sole purpose it to backport typing features to older versions of Python.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dreamsorcerer maybe we can follow up with another PR? since there's already typing_extensions installed?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's not up to me, just providing some feedback that could be adopted by the project.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'd been going with the approach of being explicit about the versions where typing_extensions is actually needed, but I realise it does add clutter and overhead. I can see the argument both ways, but when in doubt going with project consistency is generally a safe bet.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure anyone is complaining about having typing_extensions as a dependency for recent python versions. If someone opens an issue and express a use case where this is annoying we'll take a look at it. Let's not fix a problem that's not bothering anyone. Importing typing_extensions unconditionally is fine for now.

@patrick91 patrick91 marked this pull request as ready for review September 26, 2025 21:07
**kwargs: Unpack[RunArgs],
) -> str: ...

def run(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason the non-overload method doesn't use **kwargs: Unpack[RunArgs]? If it doesn't there's still duplication and a need to update two places at once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LewisGaul yes, unfortunately it is needed for docs, I didn't explain it properly in my PR message, but basically since it's not possible to give default args to kwargs we can't really avoid the duplication

@gabrieldemarmiesse
Copy link
Owner

Thanks a lot for the PR! Sorry for the delayed review, I'm currently on holiday without access to my computer. I'll take a look at the beginning of next week

Copy link
Owner

@gabrieldemarmiesse gabrieldemarmiesse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplication is acceptable, we don't degrade the UX in any way, docs or auto-completion. So yeah let's merge this and hope futur changes to the python language allow us to avoid this duplication. Thanks @Dreamsorcerer @patrick91 for your work on this!

@gabrieldemarmiesse gabrieldemarmiesse merged commit b694f85 into gabrieldemarmiesse:master Oct 13, 2025
21 checks passed
@gabrieldemarmiesse
Copy link
Owner

I'll wait for a few days, if you want to try it out and then do a release end of this week. Cheers!

@patrick91
Copy link
Contributor Author

@gabrieldemarmiesse thanks, just tested it and it works as expected!

@patrick91 patrick91 deleted the feature/overlods branch October 16, 2025 15:01
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

Successfully merging this pull request may close these issues.

4 participants