Live data from Hacker News

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

adamj.eu

61–70 of 158 posts

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

#61
post #7

Why do people not just type everything they want passed? def variable(n:str, nn:str, nnn:str, *, a:int, b:int, c:int) Anything after,*, is a kwarg.

It is used when the number of argument can vary, like: def sum(*args: int) -> int: if len(args) == 0: return 0 return args[0] + sum(*args[1:])

You should use `Iterable`

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

#62

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? https://qiskit.org/ecosystem/aer/stubs/qiskit_aer.primitives...

I have been annoyed by this too! I like how seaborn handles it now in documentation: https://seaborn.pydata.org/generated/seaborn.barplot.html?hi...

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

#63
post #25

Earlier quoted context omitted.

What do you do when inheriting from a base class with a defined __init__ ?

For everybody reading this and scratching their head why this is relevant: Python subclassing is strange. Essentially super().__init__() will resolve to a statically unknowable class at run-time because super() refers to the next class in the MRO. Knowing what class you will call is essentially unknowable as soon as you accept that either your provider class hierarchy may change or you have consumers you do not contr…

Thank you for explaining this; there are a lot of comments here suggesting trivial code style improvements for use cases where *kwargs wasn’t actually needed. The more interesting question is how to improve the use case you describe — which is how I’ve usually seen *kwargs used.

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

#64
post #44

Earlier quoted context omitted.

> Then why doesn’t a[1] return None? Because there would be no way to distinguish between "a[1] contains None" and "a[1]" doesn’t exist.

And with a[1:] returning the empty list there’s no way to distinguish between a is empty and a only has one element. These are, in the end, relatively arbitrary language design decisions.

When you slice a list, you get a list. When you see there is nothing inside the returning list, you know that means end of list, contains zero element. Slicing and indexing return object at different level.

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

#66
post #46

Why do people not just type everything they want passed? def variable(n:str, nn:str, nnn:str, *, a:int, b:int, c:int) Anything after,*, is a kwarg.

If you have enough arguments that the signature becomes obscure to read you need a dataclass to pass into the function instead. I would rather: @dataclass(frozen=True, slots=True) class VarThings: n: int ... def variable(a: VarThings): ... Than a million args

I usually start with a namedtuple unless I need the additional features provided by a dataclass.

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

#67

Earlier quoted context omitted.

And with a[1:] returning the empty list there’s no way to distinguish between a is empty and a only has one element. These are, in the end, relatively arbitrary language design decisions.

When you slice a list, you get a list. When you see there is nothing inside the returning list, you know that means end of list, contains zero element. Slicing and indexing return object at different level.

Slicing a list, when the first index is invalid for that list, could easily throw an exception instead.

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

#68
post #33

Earlier quoted context omitted.

It seems altogether surprising that with an empty list or tuple a, a[1] results in index error, yet a[1:] quietly returns an empty list or tuple.

> It seems altogether surprising that with an empty list or tuple a, a[1] results in index error, yet a[1:] quietly returns an empty list or tuple. `a[1:]` returns the sequence of elements that start at index 1. If there is no such element, the list is empty. I don’t see any good reason why this should throw an error.

Both cases are an index error. It's just for some other reasons in case of the section, the error is represented by an empty object and it's left to user to handle the result.

This could easily conceal the indexing error unless the caller code explicitly checks the length of the returned section.

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

#69
post #61
post #7

Earlier quoted context omitted.

It is used when the number of argument can vary, like: def sum(*args: int) -> int: if len(args) == 0: return 0 return args[0] + sum(*args[1:])

You should use `Iterable`

I'm not sure

    print(firstname, lastname)
for example is more readable than

    print((firstname, lastname))
especially since I would then have to write

    print((surname,))
to just print a single string.

Variadic functions are rather classic, I think Go, Rust, C and JavaScript also have them.

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

#70

For typing **kwargs there are TypedDicts https://peps.python.org/pep-0692/ If your function just wraps another you can use the same type hints as the other function with functools.wraps https://docs.python.org/3/library/functools.html#functools.w...

While functools.wraps does propagate __annotations__ by default, be aware that not all IDE-integrated type checkers handle that properly. It's easy in PyCharm, for example, to use functools.wraps such that the wrapper function is treated by the IDE as untyped. Underneath, this is because many (most?) type checkers for Python aren't actually running the code in order to access annotation information, and are instead p…

Pycharm has the worst type checker that exists today. It may have been the best a few years back but others have suppressed it considerably.

I recently switched from Pycharm to vscode which uses pyright and it's night and day on the amount of type errors it catches, it considerably improved the quality of my code and confidence during refactoring.

And to add insult to injury Pycharm doesn't even have a pyright plugin and the mypy plugin is extremely slow and buggy.

Post reply on HN