Applications Google
Menu principal

Post a Comment On: Only Python

"Type hinting in Python: focus on readability"

15 Comments -

1 – 15 of 15
Blogger dwelden said...

Your proposal seems so much more pythonic than the PEP proposals. The stuff they are showing looks like someone is trying to remake python in the image of go or c.

7:49 PM

Blogger André Roberge said...

Thank you.

7:54 PM

Blogger claudio canepa said...

In the examples your proposal looks better than the mypy style.

Still, if you add a sphinx docstring, both seems to add redundancy.

I guess I would prefer a clean signature with a sensible annotation in the docstring.

Cheers, and thanks for sharing.

5:00 AM

Blogger Adam Sah said...

+1, it's kind of like decorators and bolts onto the language instead of trying to change it. Along the wins, pylint can be easily extended to parse this, and the errors won't all have the same line numbers.

12:32 PM

Blogger Unknown said...

I am not sure what the original proposal is but for somebody like me who codes in other languages as well, why can't we have a syntax like below:

def foo(str: x) -> str:

why the following is proposed:

def foo(x: str) -> str:

To me the first syntax makes more sense.

3:39 PM

Blogger Zim the Fox said...

I have two very, very small problems with this proposal. Mind you, I am far from a Python expert.

Python is coded in blocks to make it easy to differentiate one block from another, and it works just fine. There is no need nest the type definitions two levels further from the rest of the function. It breaks current syntax for no good reason.

I also don't like that with this syntax, type definitions go from being expressions into statements. I feel as though as a Python statement should do something to affect the running program. I don't see a good workaround to that though, and your proposal is far superior to the proposed PEP.

7:42 PM

Blogger Unknown said...

I like it. Just one thing, where: doesn't need to be over-indented. It could be a simple block keyword/instruction.

Another thing I would like to see is a more coherent way of indicating types, like Any(list(str), int) or function(float, float, float).

10:21 PM

Blogger André Roberge said...

@Jose: I agree that the where block does not (in principle) need to be overindented; however, it was required by my editor so that I could use code-folding to hide it.

As to the exact way the types are declared: I figured that I should use the same as in the PEP since the point was to illustrate that the code could be made more readable if the type information was put in a code block rather than mixed in with the function arguments.

10:25 PM

Blogger Benjamin said...

I wholly agree that the proposed syntax is abominable. It's the opposite of readable. I do really like the idea of a 'where' keyword to denote typing, but I think a slight refinement of your idea would prove even better:

def retry(callback, timeout, retries=None) where
........callback: Callable[AnyArgs, Any[int, None]],
........timeout: Any[int, None],
........retries: Any[int, None],
........return None:
....pass

def greeting(name) where name: str, return str:
....return 'Hello ' + name

x = [] where x: List[Employee]

To me, this orders of magnitude more readable than the proposed nonsense.

PS. Note the 8-space indent above would only a convention, not requirement.

5:37 AM

Blogger Benjamin said...

Please pardon the spam, but I thought of more refinement when I was composing the following message to python-dev:

The proposed syntax is abominable. It's the opposite of readable.

The function annotation syntax is ugly, but potentially useful for things like documentation. While it may very well have been created with the idea of type-checking, actually using it for such quickly turns into an unreadable morass of information that is far more difficult for human brains to parse, which makes this usage the antithesis of pythonic.

I much prefer the idea of a 'where' keyword to denote typing, as discussed [here], but I think a refinement of their idea would prove even better:

def retry(callback, timeout, retries=None) where
........callback is Callable[AnyArgs, Any[int, None]],
........timeout is Any[int, None],
........retries is in [int, None], # 'is in' construct is more readable, dunno about complexity
........return is None:
....pass

def greeting(name) where name is str, return is str:
....return 'Hello ' + name

x, y = [], [] where x, y is List[Employee], List[Manager]

To me, this orders of magnitude more readable than the proposed nonsense.

PS. Obviously the 8-space indent above would only a convention, not requirement.

6:15 AM

Blogger André Roberge said...

@Benjamin:

I like your version; in some ways, it is cleaner than mine. The one relatively minor drawback is that, with your version, it becomes impossible for code-folding (which is driven by indentation in all editors I have used) to be used to hide the type declaration.

4:17 PM

Blogger Benjamin said...

I've only ever used vim or PyCharm as editors and I'm fairly certain each of them could be programmed to handle it. As for purely indent-based folders, I believe this would still work and would be syntactically identical:

def retry(callback, timeout, retries=None)
........where
............callback is Callable[AnyArgs, Any[int, None]],
............timeout is Any[int, None],
............retries is in [int, None],
............return is None:
....pass

6:08 PM

Blogger Stefano Borini said...

I like the syntax, but I am unsure... is it compatible with the python parser fundamentals?

6:07 AM

Blogger Unknown said...

I like the syntax also but why use a new keyword? May be use something like :
def foo (a, b):
as:
a:int
b:float
return:float

11:46 AM

Blogger miraculixx said...

You are absolutely right. PEP484 was a bad idea, and it attracts all the wrong people (e.g. Java folks who seem to think it's a good practice to type-hint _every_ single argument on _every_ method), then complain about the runtime not asserting the types and raising exceptions. Take this one step further and we will not only have less readable programs but also another incompatible Python version. IMO PEP484 is just another good reason not to engage with Python 3...

11:43 AM

Spammers: none shall pass.
You can use some HTML tags, such as <b>, <i>, <a>

Comments on this blog are restricted to team members.

You will be asked to sign in after submitting your comment.
Please prove you're not a robot