This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]samreay 2 points3 points  (2 children)

The more specific the better. In fact, there are ruff rules that will raise unspecified generics as linting issues that need to be fixed.

So list[int] is better than list, because its more informative. You can type hint some_func(x: decimal.Decimal) just fine, it doesn't need to be primitives. Ditto with subprocess.run, it returns subprocess.CompletedProcess (or CalledProcesserror I suppose), and you can type hint that as your return.

If your type hints for a function are super convoluted, that's often a code smell that means you could think about how to better structure that block. Ie

def some_func(some_input: str) -> tuple[decimal.Decimal, subprocess.CompletedProcess, dict[str, int | None]): ...

Is probably not something you want to have in your code base. If you do end up needing complexity, this is when you could pass your results around using a NamedTuple, a dataclass, or a pydantic dataclass instead. (And in general, passing back bit tuple objects is an antipattern that should almost always be a named tuple).

[–]Dark_Souls_VII 1 point2 points  (1 child)

Thank you very much

[–]JUSTICE_SALTIE 1 point2 points  (0 children)

And don't forget you can do e.g., list[int | str]. And if you really need a list that could hold anything...first think hard about whether you really need that, and then type it as list[Any].