TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Python Type Hints – *args and **kwargs (2021)

267 pointsby ekiauhceover 1 year ago

13 comments

SushiHippieover 1 year ago
For typing **kwargs there are TypedDicts <a href="https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0692&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0692&#x2F;</a><p>If your function just wraps another you can use the same type hints as the other function with functools.wraps <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;functools.html#functools.wraps" rel="nofollow noreferrer">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;functools.html#functools.w...</a>
评论 #37283141 未加载
评论 #37287303 未加载
评论 #37286663 未加载
评论 #37285301 未加载
评论 #37294756 未加载
dfeeover 1 year ago
I actually created a library for this!<p>Forge: forge (python signatures) for fun and profit<p><a href="https:&#x2F;&#x2F;python-forge.readthedocs.io&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;python-forge.readthedocs.io&#x2F;</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;dfee&#x2F;forge">https:&#x2F;&#x2F;github.com&#x2F;dfee&#x2F;forge</a>
refactor_masterover 1 year ago
The ability of **kwargs to leave behind no proper documentation and silently swallow any invalid arguments has made us remove them entirely from our codebase. They&#x27;re almost entirely redundant when you have dataclasses.
评论 #37282836 未加载
评论 #37283094 未加载
评论 #37288147 未加载
评论 #37283169 未加载
评论 #37282981 未加载
评论 #37287104 未加载
评论 #37285695 未加载
评论 #37284457 未加载
assbuttbuttassover 1 year ago
This does restrict all of your keyword arguments to the same type. If you have keyword arguments of different types, you&#x27;re right back to no type safety.
评论 #37282839 未加载
评论 #37283199 未加载
评论 #37282739 未加载
vorticalboxover 1 year ago
Why do people not just type everything they want passed?<p>def variable(n:str, nn:str, nnn:str, *, a:int, b:int, c:int)<p>Anything after,*, is a kwarg.
评论 #37282749 未加载
评论 #37282928 未加载
评论 #37282746 未加载
评论 #37283757 未加载
评论 #37283261 未加载
评论 #37283982 未加载
评论 #37282766 未加载
blibbleover 1 year ago
now try typing a decorator<p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;47060133&#x2F;python-3-type-hinting-for-decorator" rel="nofollow noreferrer">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;47060133&#x2F;python-3-type-h...</a><p>what a disaster
评论 #37286924 未加载
hk__2over 1 year ago
&gt; In the function body, args will be a tuple, and kwargs a dict with string keys.<p>This always bugs me: why is `args` immutable (tuple) but `kwargs` mutable (dict)? In my experience it’s much more common to have to extend or modify `kwargs` rather than `args`, but I would find more natural having an immutable dict for `kwargs`.
评论 #37285983 未加载
评论 #37286592 未加载
itissidover 1 year ago
#TIL. Also cool to know is pydantic&#x27;s @validate decorator: <a href="https:&#x2F;&#x2F;docs.pydantic.dev&#x2F;latest&#x2F;usage&#x2F;validation_decorator&#x2F;#function-signatures" rel="nofollow noreferrer">https:&#x2F;&#x2F;docs.pydantic.dev&#x2F;latest&#x2F;usage&#x2F;validation_decorator&#x2F;...</a> and in case you were thinking its not superflous to mypy(<a href="https:&#x2F;&#x2F;docs.pydantic.dev&#x2F;latest&#x2F;usage&#x2F;validation_decorator&#x2F;#usage-with-mypy" rel="nofollow noreferrer">https:&#x2F;&#x2F;docs.pydantic.dev&#x2F;latest&#x2F;usage&#x2F;validation_decorator&#x2F;...</a>).
curiousgalover 1 year ago
Is it just me or are Python type hints like..goofy?
评论 #37285735 未加载
评论 #37284843 未加载
评论 #37286569 未加载
评论 #37285790 未加载
评论 #37285606 未加载
PartiallyTypedover 1 year ago
Alternatively, use an `@overload` in a `.pyi` file and specify your types there.<p>This means that you will have 2^N combinations and doubling every time you accept a new argument.<p>If that is not good enough, then simply use a `TypedDict` with everything optional instead of `**kwargs`. Your call will then become `foo(SomeTypedDict(p1=p2,...))`.
m3047over 1 year ago
That article promulgates a misunderstanding about immutability. For my way of thinking, python is already an interpreted language and I can enforce tropes in code more cleanly and effectively than people taking something five levels up at face value and trying to figure out what sticks when they throw it against the wall: no wonder they end up frustrated, and it&#x27;s a frustrating situation.<p>Given:<p><pre><code> def foo(*args): print(args) return class Thing(object): def __init__(self,a,b): self.a = a self.b = b return def foo_style(self): return (self.a, self.b) </code></pre> <i>args is not required to refer to a tuple:<p><pre><code> &gt;&gt;&gt; foo(*[31,42]) (31, 42) </code></pre> I can have objects construct parameters conforming to the specifications for a signature:<p><pre><code> &gt;&gt;&gt; foo(*Thing(3,91).foo_style()) (3, 91) </code></pre> Consider that a counterexample.</i>
评论 #37285953 未加载
评论 #37284471 未加载
ameliusover 1 year ago
What if the second argument is a float?
评论 #37289724 未加载
akasakahakadaover 1 year ago
Although these two comes in handly, people have been using them wrong. Often in scientific open source package, they slap *kwargs in function definition without documentation. How am I suppose to know what to pass in?<p><a href="https:&#x2F;&#x2F;qiskit.org&#x2F;ecosystem&#x2F;aer&#x2F;stubs&#x2F;qiskit_aer.primitives.Estimator.set_options.html#qiskit_aer.primitives.Estimator.set_options" rel="nofollow noreferrer">https:&#x2F;&#x2F;qiskit.org&#x2F;ecosystem&#x2F;aer&#x2F;stubs&#x2F;qiskit_aer.primitives...</a>
评论 #37283034 未加载
评论 #37282965 未加载
评论 #37283638 未加载
评论 #37284592 未加载
评论 #37284455 未加载