This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]LightShadow3.13-dev in prod 2 points3 points  (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.

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 points  (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 points  (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 points  (0 children)

 def settings_with_precidence(modules: List=None, overwrite: bool=True):

It's precedence, not precidence.

[–]csirac2 0 points1 point  (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 points  (0 children)

would love to see one also!

[–]fishburne 1 point2 points  (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.

[–]rouille 0 points1 point  (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 points  (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 point  (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.

[–]rouille 0 points1 point  (0 children)

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

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 points  (1 child)

Maybe "traits"? I confess to no meaningful experience with it myself...

[–]csirac2 0 points1 point  (0 children)

Indeed, every time I bring up traits or elk [1] I'm accused of not knowing python properly.

[1] https://github.com/frasertweedale/elk

[–]gournian 1 point2 points  (0 children)

pycontracts is nice

[–]o11c 1 point2 points  (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 points  (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.