Show HN: Python-Type-Challenges, master Python typing with online exercises
python-type-challenges.zeabur.app
python-type-challenges.zeabur.app
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.
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.
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.
"all functions without a return type or parameter types will implicitly default to using Any"
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.
I'll also encourage people to contribute and make the challenges better.
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 hereThis 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.
In fact this is describe in the solution https://github.com/laike9m/Python-Type-Challenges/blob/main/...
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.
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
I like the proposal, I can change it to showing the solution link once user has run it.
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"?