Coder Social home page Coder Social logo

laike9m / python-type-challenges Goto Github PK

View Code? Open in Web Editor NEW
360.0 5.0 26.0 750 KB

Master Python typing (type hints) with interactive online exercises!

Home Page: https://python-type-challenges.zeabur.app

License: MIT License

Python 61.37% HTML 26.93% JavaScript 11.70%
python python-education python-typing type-hints

python-type-challenges's Introduction

Python Type Challenges

Master Python typing (type hints) with interactive online exercises!

Click 👉👉 HERE to start

Happy typing!

How to Run Locally

You can also run the challenge locally. To do that, clone the project and install necessary dependencies, using either PDM or requirements.txt.

PDM is recommended. After installing PDM, you can install needed dependencies with the following steps:

pdm install
pdm dev  # This will run a local Flask server

Alternatively, you can install dependencies with requirements.txt:

pip install -r requirements.txt
flask run

How to Contribute

  • Add new challenges

    Adding a new challenge is pretty simple: you only need to create a new folder, add a question.py and a solution.py, and that's it. See here for a detailed guidance.

  • New features & bug fixes

    If you want to fix a bug or add a new feature, follow the guidance.

Got Questions?

For general questions, you can post them in Discussions.

If you met issues or want to suggest a new feature/improvement, feel free to open a new issue.

Sponsor

Deployed on Zeabur

Credits

This project is inspired Type Exercise in Rust by @skyzh, and type-challenges by @antfu.

Social graph images come from Christin Hume.

python-type-challenges's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

python-type-challenges's Issues

The ellipsis in the function parameters of challenge intermediate/literalstring causes challenge failed

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.

Show challenges in 4 columns on homepage

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

image

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.

Add newline in `typealias` challenge

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.

[Formatter] Replace `black`

Description

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

Suggestion

Replace black with ruff

Consider adding a hint message with links for each question

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:

  • For basic/any: HINT: Check out Any.
  • For intermediate/callable: HINT: Check out Callable.
  • For intermediate/generic2: HINT: Check out TypeVar, constraints are needed for this question.
  • ...

Implementation

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

Return type for extreme concatenate

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?

Line numbers mismatch

Example:

image

It is a bit difficult to locate which line is incorrect.

Maybe we can split the challenge into source code part and test part.

much simpler solution to `extreme-self-casting`

this one felt a bit too easy -- I'm not sure how to change the problem to ...

spoilers
... require annotating [sic] `self`
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]:
        ...

Allow custom challenge order

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.

Record and show passed challenges

It would be nice to show the status of each challenges (passed, not passed), like Leetcode:

image

We can store the information in localStorage

Optimize server memory usage

Zeabur free plan has a memory limit of 512MB, and it appears that we're running on the edge and (according to their dev team) have OOM'ed

image

extreme-concatenate not working? (maybe outdated pyright)

this works locally -- and on the version of pyright stated in requirements.txt

spoilers
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

Enable UTF-8 mode for python (PEP 686)

Description

After pdm dev, UnicodeDecodeError(gbk: illegal multibyte sequence) caused by string ——

the string is in Python-Type-Challenges/challenges/intermediate-self/hints.md


Also, the emoji cannot load successfully
ba39653952033119fc57dcda2a265726

Solution

Python UTF-8 mode

Create a Logo

Image size should be 1200 x 630 to fit the requirement of social networks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.