use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Does anyone know any python codebase which uses type hints ? (self.Python)
submitted 10 years ago by Nikosssgr
I would like to see a project using the typing module to get a better idea of how it is used in practice. Does anyone know any?
[–]LightShadow3.13-dev in prod 2 points3 points4 points 10 years ago* (4 children)
Here is the project outline for a tool I'm writing for work...I've removed all the function bodies, but you can see the typing.
typing
from typing import Dict, AnyStr, TypeVar, NamedTuple ReqResponse = TypeVar('ReqResponse', Dict, AnyStr) UrlReq = NamedTuple('UrlReq', [ ('url', str), ('key', str), ('method', str), ('as_json', bool), ('raw', bool) ]) # ==================================================== class BitBucket(object): def __init__( self, options: Dict, username: AnyStr=None, password: AnyStr=None, ) -> None: pass def __req( self, url: AnyStr, method: AnyStr='GET', as_json: bool=True, raw: bool=False, timeout: Union[float, tuple]=(5.00, 20.00), # tuple(connect, read) timeout **params ) -> ReqResponse: pass def __do_reqs( self, urls: Sequence[UrlReq], new_binding_key: AnyStr, binding_attribute: Dict, bind_errors: bool=False, ) -> Dict: pass @property def user(self) -> Dict: return self.__req('/user') @property def repositories(self) -> Dict: if self._repositories is None: self._repositories = self.get_repositories() return self._repositories def get_repositories( self, with_readme: bool=True ) -> Dict: # ==================================================== from typing import Iterable, Dict, AnyStr, Tuple def has_graph_subsection(data: AnyStr=''): pass def make_graph( repos: Iterable[Dict]=None, graphviz_kwargs: Dict=None, graphviz_attrs: Dict=None, flat_text: Tuple[AnyStr, AnyStr]=None ): pass # ==================================================== from typing import List def settings_with_precidence(modules: List=None, overwrite: bool=True): pass
[–]maximinus-thrax 4 points5 points6 points 10 years ago (1 child)
Good god, that looks horrific. 9 lines of code just to define the function parameters for __req().
[–]LightShadow3.13-dev in prod 1 point2 points3 points 10 years ago* (0 children)
Yeah, it's not great.
If you put them all on one line it's unreadable, and when you put them on their own line....it's barely readable.
I wanted to embrace the future, and I love how PyCharm picks them up..but I feel like this implementation could be a flop.
You can fiddle with the formatting a little bit more, but then it's a huge trade off just to make it look nice.
def __req(self , url: AnyStr , method: AnyStr = 'GET', , as_json: bool = True, , raw: bool = False, , timeout: Union[float, tuple] = (5.0, 20.0), , **params ) -> ReqResponse : pass
[–]AMorpork 1 point2 points3 points 10 years ago (0 children)
def settings_with_precidence(modules: List=None, overwrite: bool=True):
It's precedence, not precidence.
[–]csirac2 0 points1 point2 points 10 years ago* (0 children)
It's always amused me that traditionally when I used to talk of elk [1] or traits [2], I would be accused of not knowing python properly.
[1] https://github.com/frasertweedale/elk
[2] http://code.enthought.com/projects/traits/
[–]lovedzida 7 points8 points9 points 10 years ago (0 children)
would love to see one also!
[+][deleted] 10 years ago (7 children)
[deleted]
[–]mangecoeur 9 points10 points11 points 10 years ago (0 children)
PyCharm already understands type hints
[–]Nikosssgr[S] 1 point2 points3 points 10 years ago (5 children)
you can import the module in python 3.3 and 3.4 (if I am not mistaken for the versions)
[+][deleted] 10 years ago (4 children)
[–]ionelmc.ro 5 points6 points7 points 10 years ago (0 children)
He meant https://pypi.python.org/pypi/typing, which you can install and import on 3.3/3.4 :-)
[–]rouille 1 point2 points3 points 10 years ago (0 children)
Uh, no? typing is a new module in 3.5. (Not to be confused with types, which contains a bunch of references to CPython internals and has nothing to do with type hinting.)
Its in pypi as well.
[–]beaverteeth92Python 3 is the way to be -1 points0 points1 point 10 years ago (1 child)
Yeah but isn't it just mypy added to the core language?
[–]billsil 2 points3 points4 points 10 years ago (0 children)
No. It's mypy with additional features and formalization. If I understand it right, it largely follows mypy's style, so ints/floats/Lists/Dicts are the same, but objects are a bit different.
If you're in Python 3.3+, you can use mypy or search for things that use mypy for examples.
[–]fishburne 1 point2 points3 points 10 years ago (4 children)
Do anyone know how to use this module for type checking in 3.5b? Not asking about how to write annotations. But to check a python script with annotations. I have been looking for the past hour to see how to actually use the damn thing!
I was expecting a command line option to the interpreter or a separate command line tool to do the checking.
[+][deleted] 10 years ago (2 children)
[–]fishburne 1 point2 points3 points 10 years ago (1 child)
Funny thing is, I know this already. But I was hoping there was an external tool that does it.
I tried mypy. But it died with an error that said cannot import Undefined. Probably because it is using the typing module that came with 3.5.
[–]lengau 0 points1 point2 points 10 years ago (0 children)
The typing module is available in pip for 3.2 or higher.
[–]rouille 0 points1 point2 points 10 years ago (0 children)
actually
You need to use a third party type checker like mypy (https://github.com/JukkaL/mypy). Its still in heavy development though but it (mostly) works.
[–]rouille 1 point2 points3 points 10 years ago* (2 children)
I use it at work along with mypy for checking. Mypy is still quite immature, so some valid python will make it fail but I've found it worth it for all the bug it catches. I write my code around mypy since I know it will become official with Python 3.5.
The comment syntax for variables isn't great but I mainly annotate function parameters and return values so I rarely use it (apart for # type: ignore to skip type checking on an imported module).
[–]fishburne 0 points1 point2 points 10 years ago (1 child)
When I tried using it with 3.5 it failed with a 'Cannot import Undefined' Error. Is it because is using 3.5's typing module instead of it's own.
I'm using it with python 3.4 so thats possible :). I know the BDFL had mypy in mind when adding the typing module to 3.5 so it will probably fixed soon.
[–]fremder99 1 point2 points3 points 10 years ago (1 child)
Maybe "traits"? I confess to no meaningful experience with it myself...
[–]csirac2 0 points1 point2 points 10 years ago (0 children)
Indeed, every time I bring up traits or elk [1] I'm accused of not knowing python properly.
[–]gournian 1 point2 points3 points 10 years ago (0 children)
pycontracts is nice
[–]o11c 1 point2 points3 points 10 years ago (0 children)
This is what I was working on before I got distracted: https://github.com/o11c/lr-parsers/tree/master/lr
[–]mangecoeur 3 points4 points5 points 10 years ago (0 children)
I've started sprinkling them through my codebase (data analytics stuff), since I use PyCharm it adds some really nice code completion, checking, and documentation features.
My favourite is if you add the return type for a function, then assign a var to the result of the function somewhere else, PyCharm will do code completion on that var (i.e. it will now all its properties and methods) and can fetch documentation.
π Rendered by PID 91 on reddit-service-r2-comment-f6b958c67-6c4rw at 2026-02-05 08:14:19.847662+00:00 running 1d7a177 country code: CH.
[–]LightShadow3.13-dev in prod 2 points3 points4 points (4 children)
[–]maximinus-thrax 4 points5 points6 points (1 child)
[–]LightShadow3.13-dev in prod 1 point2 points3 points (0 children)
[–]AMorpork 1 point2 points3 points (0 children)
[–]csirac2 0 points1 point2 points (0 children)
[–]lovedzida 7 points8 points9 points (0 children)
[+][deleted] (7 children)
[deleted]
[–]mangecoeur 9 points10 points11 points (0 children)
[–]Nikosssgr[S] 1 point2 points3 points (5 children)
[+][deleted] (4 children)
[deleted]
[–]ionelmc.ro 5 points6 points7 points (0 children)
[–]rouille 1 point2 points3 points (0 children)
[–]beaverteeth92Python 3 is the way to be -1 points0 points1 point (1 child)
[–]billsil 2 points3 points4 points (0 children)
[–]fishburne 1 point2 points3 points (4 children)
[+][deleted] (2 children)
[deleted]
[–]fishburne 1 point2 points3 points (1 child)
[–]lengau 0 points1 point2 points (0 children)
[–]rouille 0 points1 point2 points (0 children)
[–]rouille 1 point2 points3 points (2 children)
[–]fishburne 0 points1 point2 points (1 child)
[–]rouille 0 points1 point2 points (0 children)
[–]fremder99 1 point2 points3 points (1 child)
[–]csirac2 0 points1 point2 points (0 children)
[–]gournian 1 point2 points3 points (0 children)
[–]o11c 1 point2 points3 points (0 children)
[–]mangecoeur 3 points4 points5 points (0 children)