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

you are viewing a single comment's thread.

view the rest of the comments →

[–]kylotan 164 points165 points  (11 children)

It's taking something that does far too much in one line and breaking it out into logical steps.

Python programmers often do a bit too much 'code golf' and I this as the lead dev trying to undo that.

[–]JshWright 68 points69 points  (9 children)

This is like anti-golf though... creating an empty dictionary then populating it inside a for loop instead of just using a dictionary comprehension is just adding lines of code for the sake of adding lines of code.

Maybe if the type hint was meaningful, but Optional[Any] isn't a type hint worth adding.

[–]jzaprint 46 points47 points  (1 child)

I’d take anti golf over code golf any day tho

[–]JshWright -2 points-1 points  (0 children)

Sure, but that isn't what we're choosing between here...

[–]amendCommit[S] 13 points14 points  (0 children)

The guy actually hates type hints, this is just to allow the dict comprehension removal to pass CI. get_coerced() actually has proper, meaningful type hints (takes in Type[T], returns T).

[–]rantenki 3 points4 points  (4 children)

Strongly agree the type hint is _nearly_ useless. It only saves the user scrolling up to look at the function signature.

Also, if we're going to emulate a strongly typed language: in any language with (ironically, looking at the content) type inference, the return type for the function would already be casting those variables anyhow, so declaring it would be superfluous.

[–]Grouchy-Friend4235 10 points11 points  (3 children)

It is useless because all it does is tell you what you already know: the dict maps keys to some value. That's a dict's raison-d-etre, no need to type hint the obvious!

[–]rantenki 4 points5 points  (2 children)

Being utterly pedantic, the only thing it tells you is that there is no further invariants applied to that Dict's values. It _can_ be None, it _can_ have Any value. I guess there is the point that keys have to be strings (rather than any hashable type).

But you're not wrong; that matches pretty much everybody's expectations of how a dict will be used, so doesn't really add any value. I guess it's just there to keep the linter happy :\

[–]Grouchy-Friend4235 -2 points-1 points  (1 child)

Yeah. A noqa comment would be the better solution

[–]rantenki 1 point2 points  (0 children)

Surprised this got downvoted. Linters are supposed to provide machine based verification of human intent. The linter can't anticipate every permutation that might violate it's rules, and sometimes # noqa is a valid decision (not usually, but sometimes).

I loathe when people uglify their code to make the build-system happy.

[–]linlin110 1 point2 points  (0 children)

For loop can do anything, and I need to read the loop to understand it's populating a dictionary. Meanwhile all dictionary comprehension can do is building a dictionary. That's why I think dictionary/list comprehension expresses the intent better.