Settings

Theme

Show HN: Python-Type-Challenges, master Python typing with online exercises

python-type-challenges.zeabur.app

110 points by laike9m 2 years ago · 47 comments · 1 min read

Reader

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

vsnf 2 years ago

I 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.

  • roland35 2 years ago

    Well, that is realistic :D I agree it is a pain point with python typing, but it is something good to learn in this activity and remember honestly!

  • burning_hamster 2 years ago

    I 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 2 years ago

      Also, a "Next example" button would be great.

      • laike9mOP 2 years ago

        I definitely considered adding hints. But it's gonna be a ton of work to create proper hints and I'm yet to prioritize it.

  • calibas 2 years ago

    I assume that's intentional so you learn when it's required. From Python.org:

    "all functions without a return type or parameter types will implicitly default to using Any"

  • hsfzxjy 2 years ago

    I passed the test with `object` :)

    • quietbritishjim 2 years ago

      I literally didn't change the code, so there was no annotation at all, and it still passed.

      • crunchbang-excl 2 years ago

        I thought that one was an example where you don't have to do anything.

        I just went back to check and I saw that you are supposed to make some changes, which are shown in the solution, even though they are unnecessary to pass the tests.

        • laike9mOP 2 years ago

          The "any" challenge is special, it's the only one where you don't need to make any changes :)

  • georgehotelling 2 years ago

    I opened an issue asking them to add `from typing import *` to the boilerplate.

  • v3ss0n 2 years ago

    you aren't wrong , any is available in python 3.10 . There is no need to import typing.Any

d4rkp4ttern 2 years ago

I 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 ?

burning_hamster 2 years ago

I 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.

  • laike9mOP 2 years ago

    That's great to know. I actually made it so I can really learn and understand Python typing.

Arch-TK 2 years ago

It 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.

  • laike9mOP 2 years ago

    Fair enough. I'll refine the solution to avoid direct copies.

    I'll also encourage people to contribute and make the challenges better.

Joeboy 2 years ago

I'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 2 years ago

    It takes into account the recent changes. At the top of the window it indicates the Python's version used.

crunchbang-excl 2 years ago

The 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.

dddnzzz334 2 years ago

I am unable to do 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.

  • laike9mOP 2 years ago

    Sorry about the confusion. This issue has been fixed. New line is not needed anymore.

nilslindemann 2 years ago

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.

HNArg024 2 years ago

Very 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

  • laike9mOP 2 years ago

    As 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.

hiAndrewQuinn 2 years ago

I could use one of these for just about every language I work in. Go, Haskell, Python, TypeScript...

nicolaslem 2 years ago

This is amazing and should be part of the Python documentation.

caditinpiscinam 2 years ago

It 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.

satyanash 2 years ago

This is quite useful for training people quickly and understanding how certain parts of the python `typing` module work. Thanks for making this.

Probiotic6081 2 years ago

Interesting idea but I'll stick to my regular 10fastfingers training regime

mock-possum 2 years ago

Anyone know of anything similar, only in Typescript? Typing Koans?

v3ss0n 2 years ago

it don't mention python version. `any` is available by default in python 3.10 +

  • karamanolev 2 years ago

    Those are completely different.

    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 2 years ago

    The typing exercise should say Any rather than any, and similarly for other type names.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection