Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Show HN: Python-Type-Challenges, master Python typing with online exercises (zeabur.app)
110 points by laike9m on Nov 29, 2023 | hide | past | favorite | 46 comments
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




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.


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!


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?


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


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.


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"


I passed the test with `object` :)


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


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.


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


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


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


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.


Let's not forget ANY as in "from unittest.mock import ANY".


Whoops, I always thought I can use any as type. Damn my bad


I still think callable, and iter should be usable this way.

any(), probably not. The semantic is too different.

But having to import Callable while callable is a built-in is a missed opportunity.

I don't miss Union, Optional and List.


what? `any` - the builtin function[0] is part of python since the dawn of time but from what I know is not usable as a type annotation, and `typing.Any` should be used (and imported explicitly)

[0] https://docs.python.org/3/library/functions.html#any


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 ?


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.


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


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.


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

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


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.


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


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.


Your 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/...


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.


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


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.


emm, the default value can be anything really...


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


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.


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



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


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.


Every problem has a link to its solution underneath the editor area.


Only after you fail to create a working solution yourself.


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


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


:)


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



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


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


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




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: