laike9m / python-type-challenges Goto Github PK
View Code? Open in Web Editor NEWMaster Python typing (type hints) with interactive online exercises!
Home Page: https://python-type-challenges.zeabur.app
License: MIT License
Master Python typing (type hints) with interactive online exercises!
Home Page: https://python-type-challenges.zeabur.app
License: MIT License
this one felt a bit too easy -- I'm not sure how to change the problem to ...
from typing import *
class Fn[R, **P]:
def __init__(self, f: Callable[P, R]):
self.f = f
def transform_callable(self) -> Callable[Concatenate[object, P], R]:
...
As we have random challenge entrance, we can add it to README.
It is also necessary to explicitly specify the Python version in the context, otherwise a lot of the solving will be affected.
this works locally -- and on the version of pyright stated in requirements.txt
from typing import Protocol
class Person:
pass
class P1[R, **P](Protocol):
def __call__(self, value: object, *args: P.args, **kwargs: P.kwargs) -> R: ...
class P2[R, **P](Protocol):
def __call__(self, value: Person, *args: P.args, **kwargs: P.kwargs) -> R: ...
def transform[R, **P](f: P1[R, P]) -> P2[R, P]:
def wrapper(value: Person, *args: P.args, **kwargs: P.kwargs) -> R:
return f(value, *args, **kwargs)
return wrapper
peeked at the stated solution and it feels a little cheeky to rely on the type checker to fill in the return value there
I think it related with #92
Currently, there is no hint message for most questions. For a few questions like callable-protocol, there is a short "HINT: Use Protocol" written in the code, but there is no standard.
We do provide a "Solution" button, but it's very different from a hint. When the user is playing with each challenge, a well-designed hint message can guide him and help him complete the question by learning, but the "solution" button takes him directly to the answer--It's faster but the user may not learn anything at all!
So I'm suggesting that we add a hint message to all questions. The hint messages follow a standard format, like this:
basic/any
: HINT: Check out Any.intermediate/callable
: HINT: Check out Callable.intermediate/generic2
: HINT: Check out TypeVar, constraints
are needed for this question.The hint can be added directly in any question.py
like this:
## HINT start ##
(message in Markdown format)
## HINT end ##
The server can extracts the message and render it as HTML(to make hyper link works).
something like
basic intermediate advanced extreme
basic-1 intermediate-1 advanced-1 extreme-1
basic-2 intermediate-2 advanced-2 extreme-2
...
Right now it's a single column, which doesn't look good
For implementation, we won't be reusing the whole challenge_list
component, but we perhaps can make a component to represent challenges belonging to a single level, and reuse it.
Test should cover returning challenges from backend, and sending user code to backend and get type check result
@100gle How did you get the Python svg logo? I assume you used https://icon-sets.iconify.design/logos/python/ ?
Right now there are 4 levels
Though there's no predefined criterias for determing a challenge's level.
It seems pretty reproducible, but I'm not sure if this is our problem or theirs
The following fails to work on the typealias
challenge:
"""
TODO:
Create a new type called Vector, which is a list of float.
"""
type Vector = list[float]
with the error:
Error:
7:error: Statements must be separated by newlines or semicolons
This took me a second, and the solution that's accepted is in fact:
"""
TODO:
Create a new type called Vector, which is a list of float.
"""
type Vector = list[float]
Note the trailing newline (I had to add a space as well because the GH markdown parser seems to swallow it otherwise).
I haven't looked at the code, but I suspect it's concatenating the left and right code samples without adding a newline, so this could come as a surprise to some.
It would in general maybe be better to just strip both the LHS and the RHS of any trailing whitespace, and concatenate them with a newline in the middle, just to avoid issues with indentation and newlines.
def f(a: list[int | str]):
pass
def g(a: str | list):
pass
Maybe we can use definition to make challenge strict.
pyright supports switch python version with command line option --pythonversion
Currently the challenges appear in alphabetical order, it would be better if we customize the order by prioritizing the easier challenges to the top. For example, the basic challenges could use this order:
# The basic syntax
- parameter
- return
- variable
- any
- kwargs
# Composite data types
- list
- dict
- tuple
# Other
- typealias
- union
- optional
- final
This would make the experience better as people go through the challenges from top to bottom.
solution.py in recursive uses TypeAlias which is deprecated since 3.12.
Image size should be 1200 x 630 to fit the requirement of social networks
I imagine that imports were left out to avoid giving clues, but it would be nice to have all the type hints imported into the namespace.
PEP 696 defaults for type variables
Scenario:
I write some codes, it does not work so I want to start over again. I want to remove all my code quickly and start from the original codes.
In your solution to the extreme concatenate problem you propose:
def transform[T, **P](f: Callable[Concatenate[Any, P], T]):
def wrapper(value: Person, *args: P.args, **kwargs: P.kwargs) -> T:
return f(value, *args, **kwargs)
return wrapper
But if I add the return type of Callable[Concatenate[Person, P], T]
to the transform function, the test in line 19 fails.
Is there a better return type or do you recommend skipping it in general?
When I click the button online, the server return a 500 error.
However, I can run locally, it seems not error for python.
Or 84551ad cause?
In challenge intermediate/literalstring, function execute_query
has a parament accepted Iterable[str]
,and set default value ...
. The same pattern is observed in the provided solution.
def execute_query(sql, parameters: Iterable[str] = ...):
"""No need to implement it"""
This will cause a type check error by pyright
error: Expression of type "ellipsis" cannot be assigned to parameter of type "Iterable[str]"
It seems like I must access type check by changing Iterable[str]
to Iterable[str] | EllipsisType
, like
from typing import Iterable, LiteralString
from types import EllipsisType
def execute_query(sql: LiteralString, parameters: Iterable[str] | EllipsisType = ...):
"""No need to implement it"""
or change ...
to ()
for type check.
from typing import Iterable, LiteralString
from types import EllipsisType
def execute_query(sql: LiteralString, parameters: Iterable[str] = ()):
"""No need to implement it"""
I don't think either of the above solutions to be the best way for this issue, but I don't know any better way to use ellipsis for default parameter with type hint.
Hello,
I was trying to solve the last challenge.
this one
but I was getting strange errors in the output, so
I decided to test out the real solution but again, pasting & running the solution produced errors:
In black 24.1.0, many challenges will be formatted by black
Dummy class and function implementations consisting only of ... are formatted more
compactly
class Undergraduate:
...
It will be replaced with
class Undergraduate: ...
In solution.py
, user should complete ...
class Undergraduate(Student):
major: str
Replace black
with ruff
In case people accidentally changed on but forget about the other
pyright only reports errors in test code, which is what we expected
This issue should be fixed once we migrate to pyright (#9)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.