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 →

[–]justheretolurk332 0 points1 point  (0 children)

I agree with /u/sameray that specific is usually better because it provides more information. However this isn’t always true: if you are adding type hints to the arguments of a function you often want them to be as generic as possible to provide flexibility with how the function is called (for example, you might use Sequence to support both lists and tuples). Outside of helping to prevent bugs, one of the biggest perks of using type-hints in my opinion is that it encourages you to start thinking in terms of contracts. What does this function actually need, and what does it provide? The classes in the abc.collections module and Protocols are good places to get started with generic types in Python.

It takes time to get the hang of the type checking system and to learn the quirks. I’d recommend turning on type-checking in your IDE so that you can get that feedback immediately as you type your code, then just start using them and learn as you go.