GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — What to do when I need to unpack a dictionary? · python/typing · Discussion #1902

What to do when I need to unpack a dictionary? #1902

Unanswered
Kludex asked this question in Q&A
What to do when I need to unpack a dictionary? #1902
@Kludex Kludex
Dec 18, 2024 · 2 comments · 5 replies
Return to top
Discussion options
  • Uh oh!

    There was an error while loading. Please reload this page.

  • Uh oh!

    There was an error while loading. Please reload this page.

Kludex
Dec 18, 2024

I'm trying to add type hints to https://github.com/open-telemetry/opentelemetry-python-contrib, and in a lot of places we have a pattern that is something like:

def to_type_check(fn, args, kwargs):
    return fn(*args, **kwargs)

How do I add type hints to this?

Some playgrounds:

  • mypy
  • pyright

Also, I can't change how the code looks like, I can only add type hints. It's an old code source.

You must be logged in to vote
All reactions

Replies: 2 comments · 5 replies

Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

TeamSpen210
Dec 18, 2024

Unfortunately ParamSpec only works with exactly *args, **kwargs as inputs, so this isn't possible. If you restrict to only accepting positional arguments variadic generics would work. If these functions are only being used in the implementation and externally it just looks like the original function, perhaps you could just pretend the functions are aliases to users, and leave the implementation untypeable.

You must be logged in to vote
All reactions
5 replies
@Kludex
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

Kludex Dec 18, 2024
Author

Unfortunately ParamSpec only works with exactly *args, **kwargs as inputs, so this isn't possible. If you restrict to only accepting positional arguments variadic generics would work.

I'm aware of those points, but I'm asking for advice on what I would need to do to here to have it typed correctly.

If the answer is: "it's not possible". Then it seems like something is missing to Python itself, and I'd like to see if we can do something about it.

All reactions
@Daverball
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

  • Uh oh!

    There was an error while loading. Please reload this page.

Daverball Dec 30, 2024

That is the answer, the most accurate way to type this currently would be something along the lines of:

def to_type_check[T](fn: Callable[..., T], args: Sequence[Any], kwargs: Mapping[str, Any]) -> T:
    return fn(*args, **kwargs)

ParamSpec is indeed a very incomplete solution and yet it took a year for support in mypy to land and in pytype there's still no full support to this day. So this should give you an idea how complex the feature is to implement, even with all the simplifications to keep the scope small. I don't think anybody would object to making ParamSpec more powerful, but it will take a tremendous time investment of figuring out all the important details and corner cases, so you end up with something that's still actually sound and useful.

TypeVarTuple is slightly more powerful, so the same function could be typed accurately for passing only positional arguments:

def to_type_check[T, *Ts](fn: Callable[*Ts, T], args: tuple[*Ts]) -> T:
    return fn(*args)
All reactions
@Kludex
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

Kludex Dec 30, 2024
Author

def to_type_check[T](fn: Callable[..., T], args: Sequence[Any], **kwargs: Mapping[str, Any]) -> T:
    return fn(*args, **kwargs)

This is wrong. Are the ** an oversight?


That is the answer, [...]

I tried to be very objective in this discussion and not point to any direction. Again, if the answer is: "it's not possible". Then it seems like something is missing to Python itself, and I'd like to see if we can do something about it.

I don't think "but it will take a tremendous time investment of figuring out all the important details and corner cases" is a blocker here. I'm asking how can I proceed here, if that's the case.

All reactions
@Daverball
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

Daverball Dec 30, 2024

Indeed, the ** were added accidentally, I edited the post.

I also don't think it's a blocker, I'm just warning you that it will not be easy. Many people have asked for extended ParamSpec features, but nobody has actually invested the time yet to write up a detailed proposal, ideally with a proof-of-concept implementation in one of the type checkers.

All reactions
@Daverball
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

  • Uh oh!

    There was an error while loading. Please reload this page.

Daverball Dec 30, 2024

As far how to proceed, here's the general process for proposing larger changes to typing spec (this will almost certainly require a PEP, the process for typing PEPs is no different from other PEPs and is documented elsewhere): https://github.com/python/typing-council?tab=readme-ov-file#decisions

All reactions
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

ankittuli-debug
Oct 1, 2026

Hello to Everyone,

I am an advocate practicing in Delhi(India) for last more than 20 years. I have just started learning python from YouTube and I have installed vs code I try to code and just trying to do it. I also wrote down extensive handwritten notes on what I learn in coding. Any suggestions? I don't have any mentor and just trying to code daiktfor about 30 - 45 minutes. If I remain consistent, will i be able to become a python coder.

You must be logged in to vote
All reactions
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
4 participants
@Kludex @Daverball @TeamSpen210 @ankittuli-debug
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.