kracekumar / python-typing-koans Goto Github PK
View Code? Open in Web Editor NEWPython typing koans to learn type-hints in Python3 using Mypy
Python typing koans to learn type-hints in Python3 using Mypy
It look like this example has to be rewritten in the following manner (snippet is typed):
# Annotate the resp_klass as the type of response class
# Documentation: https://docs.python.org/3/library/typing.html?highlight=typing#typing.Type
def make_response(data: JSONData, resp_klass: Type[JSONResponse] = JSONResponse) -> bytes:
return resp_klass(data).make_response()
I guess that in this case the code has some meaning.
Also the is an unused import Union
from typing
.
Hey!
Thank you for creating this repository and responding so swiftly over chat. This set of Koans helped me to better understand type hints in Python
There were some sections in which my solutions seemed a little hacky and would like to know how others have solved it and how I might improve my own solutions. I'm still working on the koans and there are about 6 Koans left to be done though. Will resume working on this tomorrow.
Here are my solutions: https://github.com/J0/python-typing-koans
Thanks again! This is a great repo.
My might say that dry-python/returns
is one of the most typing-intensive project in Python-land.
It might be a good idea to add some expert-level koans about it. Like:
classes
can also be useful if one wants to work with typeclasses in Python: https://sobolevn.me/2021/06/typeclasses-in-python
I don't have the timing capacity to contribute this myself, but I would love to help ๐
choice
at line 18 and name
at line 21 are undefined inside the function get_random_user_data
.
Hey @kracekumar,
Thanks for taking the time to reply to all my past queries. I'm a little stuck on 114 and was wondering if you could provide a hint or suggestion of sorts. I'm currently using overloaded method signatures for sock_type
in the following format:
@overload
def _sock_type(addr: Tuple[str,int])-> Union[TCPSocket,TCP6Socket]:...
@overload
def _sock_type(addr: Union[str, bytes])-> UnixSocket:...
def _sock_type(addr: Address):
<implementation details>
However, this solution still uses union and unfortunately it also means that sock_type in the line below is TCP Socket, TCP6Socket or Unix Socket whic produces the type error sock_type
is not callable.
sock = sock_type(address, conf, log)
Was wondering if you have any hints/suggestions/tips.
Let me know
For task 110 I had to rename the last variable from user
to new_user
as I'd previously received an error stating that we can't assign a type to a variable which already has a type. Would just like to check if there's an alternative solution which doesn't involve renaming the variable.
Here's the relevant line: https://github.com/J0/python-typing-koans/blob/main/koans/py/110-easy-class-variable.py#L66
The comments mention another way of typing the solution. My solution is as follows:
"""
Koan to learn annotating the Python list of various type of elements
"""
from typing import Union, List
# Annotate mixed type-values. There are two-ways to annotate this
nos: list[Union[int, float]] = [1, 2.0, 3.5]
# Annotate the list of int to list of better types
squares: list[Union[int, float]] = [no * no for no in nos]
I suppose this could be one way to annotate. Would just like to check what the other way is.
Would just like to verify solution
Here's mine: https://github.com/J0/python-typing-koans/blob/main/koans/py/117-easy-protocol-across-objects.py
Happy to hear if others have cleaner solutions.
Hi! I see that you are already using django-stubs
and drf-stubs
which is awesome!
But, looks like this koan does miss plugin definitions: https://github.com/kracekumar/python-typing-koans/blob/main/koans/dj_koans/mysite/mypy.ini
Is it intended?
Thanks!
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.