20 ms·
Show HN: Python-Type-Challenges, master Python typing with online exercises
Hi HN, I'm excited to share Python-Type-Challenges, a collection of hands-on, interactive challenges designed to help Python developers master type annotations. Whether you're new to type hints or looking to deepen your understanding, these exercises provide a fun and educational way to explore Python's type system. I'd love to get your feedback and contributions!
https://github.com/laike9m/Python-Type-Challenges https://github.com/laike9m/Python-Type-Challenges
- Probiotic6081 3y agoInteresting idea but I'll stick to my regular 10fastfingers training regime
- laike9m 3y ago:)
- burning_hamster 3y agoI thought I knew at least the basics of typing in python but I learnt 3 new things in the first 5 minutes. Kudos, great work.
- laike9m 3y agoThat's great to know. I actually made it so I can really learn and understand Python typing.
- satyanash 3y agoThis is quite useful for training people quickly and understanding how certain parts of the python `typing` module work. Thanks for making this.
- mock-possum 3y agoAnyone know of anything similar, only in Typescript? Typing Koans?
- delfaras 3y agoI've had some fun with https://github.com/type-challenges/type-challenges https://github.com/type-challenges/type-challenges or https://bigfrontend.dev/typescript https://bigfrontend.dev/typescript
- vsnf 3y agoI got frustrated on the very first example (Basic 1: Any) because I didn't realize that the typings library wasn't imported by default. Maybe I should have known better, but it definitely got me.
- burning_hamster 3y agoI made the same error. OP, maybe instead of only giving the option to see the solution, also give the option to just see a hint?
- burning_hamster 3y agoAlso, a "Next example" button would be great.
- laike9m 3y agoI definitely considered adding hints. But it's gonna be a ton of work to create proper hints and I'm yet to prioritize it.
- v3ss0n 3y agoyou aren't wrong , any is available in python 3.10 . There is no need to import typing.Any
- karamanolev 3y agoThose are completely different. any - https://docs.python.org/3/library/functions.html#any https://docs.python.org/3/library/functions.html#any - Return True if any element of the iterable is true. Any - A special kind of type is Any. A static type checker will treat every type as being compatible with Any and Any as being compatible with every type.
- nicolaslem 3y agoLet's not forget ANY as in "from unittest.mock import ANY".
- hiAndrewQuinn 3y agoI could use one of these for just about every language I work in. Go, Haskell, Python, TypeScript...
- dotneter 3y agoFor TypeScript there is https://github.com/type-challenges/type-challenges https://github.com/type-challenges/type-challenges
- nicolaslem 3y agoThis is amazing and should be part of the Python documentation.
- v3ss0n 3y agoit don't mention python version. `any` is available by default in python 3.10 +
- karamanolev 3y agoThose are completely different. any - https://docs.python.org/3/library/functions.html#any https://docs.python.org/3/library/functions.html#any - Return True if any element of the iterable is true. Any - A special kind of type is Any. A static type checker will treat every type as being compatible with Any and Any as being compatible with every type. error: Function "builtins.any" is not valid as a type [valid-type] note: Perhaps you meant "typing.Any" instead of "any"?
- throwaway81523 3y agoThe typing exercise should say Any rather than any, and similarly for other type names.
- Joeboy 3y agoI'm ashamed to say I haven't kept up, but haven't there been big changes in typing over the last few python versions? Eg. there's a new syntax for generic types in 3.12.
- Kydlaw 3y agoIt takes into account the recent changes. At the top of the window it indicates the Python's version used.
- d4rkp4ttern 3y agoI love the idea of these types of “unit” exercises, and koans. I am surprised there aren’t more of these. In fact koan/kata like exercises can be a great complement to documentation for open source projects. I was recently looking for a good platform to create these types of exercises. Anybody have a favorite ?
- Arch-TK 3y agoIt would be improved if the problems weren't just direct copies from the python documentation. For example, I was unaware of TypedDict so I searched the typing page for "typeddict" assuming there would likely be something like that. What I got was an example containing the exact solution. I mean, I definitely learned something new but I think it would have been better if I had to come up with my own solution on the basis of the documentation and example.
- laike9m 3y agoFair enough. I'll refine the solution to avoid direct copies. I'll also encourage people to contribute and make the challenges better.
- deleted 3y ago[deleted]
- HNArg024 3y agoVery cool OP! Just a suggestion. I would like to always have the option to see the solution link, or at least, once I pass print what was your expected solution. Like, for the optional challenge I wrote foo(x: Optional[int] = None) and it passed but your solution was simpler with x: int|None = 0
- laike9m 3y agoAs long as it passed, it's considered a valid solution. The provided solution is only for reference. I like the proposal, I can change it to showing the solution link once user has run it.
- crunchbang-excl 3y agoThe challenge generic class can be solved without making any change to the pop() function. I got it to work with this code: # Code starts here class Stack[T]: def __init__(self) -> None: self.items: list[T] def push(self, item: T) -> None: self.items.append(item) def pop(self): return self.items.pop() # Code ends here This code has fewer changes than the solution provided, and doesn't need imports. I'm not sure the solution needs those imports either. Perhaps the tests for that challenge should cover more cases.
- laike9m 3y agoYour solution is fine. Pyright can infer the return types, so you don't need to annotate the return type. In fact this is describe in the solution https://github.com/laike9m/Python-Type-Challenges/blob/main/challenges/advanced-generic-class/solution.py#L23 https://github.com/laike9m/Python-Type-Challenges/blob/main/...
- caditinpiscinam 3y agoIt would be nice if this included solutions, so that it could be used as a reference in addition to training. But overall it looks well put together.
- Konnstann 3y agoEvery problem has a link to its solution underneath the editor area.
- still_grokking 3y agoOnly after you fail to create a working solution yourself.
- dddnzzz334 3y agoI am unable to do https://python-type-challenges.zeabur.app/intermediate/callable https://python-type-challenges.zeabur.app/intermediate/calla... I am doing type SingleStringInput = Callable[[str], None] but it doesn't seem to work :( EDIT: I got the problem. You need a new line at the end since it seems as if it is concatenating my last line to the first line of the test.
- laike9m 3y agoSorry about the confusion. This issue has been fixed. New line is not needed anymore.
- nilslindemann 3y agohttps://python-type-challenges.zeabur.app/basic/optional https://python-type-challenges.zeabur.app/basic/optional could have a better description. It does not become clear that the default value should be 0.
- laike9m 3y agoemm, the default value can be anything really...