Skip to content

Conversation

@henryiii
Copy link
Contributor

Comprehensions.

@notatallshaw
Copy link
Member

I don't really agree with this one, at least when it comes with dictionaries, when you're passing dictionaries to be used as arguments I much prefer the dict(foo=bar) syntax over the {"foo": bar} syntax.

Not only does it read closer to the keyword parameters your passing it forces the keword names to be valid.

@henryiii
Copy link
Contributor Author

henryiii commented Nov 12, 2025

For general use, I strongly prefer the literal over a dict call; literals are native syntax, about 30% faster, and support arbitrary keys; they are the only candidate for the "one obvious way to do it". However, I see the .evaluate usage, which IMO is pretty much a toss up, as the one place the dict(**kwargs) shortcut is rather nice is for keyword arguments. Though, to be fair, it's still less powerful:

>>> def f(**kwargs):
...     print(kwargs)
...
>>> f(**{"class": 1})
{'class': 1}
>>> f(**{"1": 1})
{'1': 1}

That is, Python itself doesn't enforce valid keyword names. :)

IMO, since there are just two cases of this, with just one argument:

- assert Marker('"2.7.9" < "foo"').evaluate(dict(foo="2.7.10"))
+ assert Marker('"2.7.9" < "foo"').evaluate({"foo": "2.7.10"})

I think adding the check with the changes is fine, but I'd also be fine to skip those two, or even disable the check on the tests folder (but still apply the from_dict one, as that's clearly better).

@notatallshaw
Copy link
Member

For general use, I strongly prefer the literal over a dict call; literals are native syntax, about 30% faster, and support arbitrary keys;

Ah, I should have made clear, I agree in general that literal syntax is preferable. But I don't agree in all cases, and to me all three examples here are more readable as dict calls.

they are the only candidate for the "one obvious way to do it".

I believe the actual text in the Zen is intended to be ironic, the em-dashes are inconsistently spaced.

And the rule here, C408, has a specific options just for this case: allow-dict-calls-with-keyword-arguments, so it seems I'm not the only one who feels this way.

@henryiii henryiii force-pushed the henryiii/chore/ruff-C4 branch from 8607681 to 54cff86 Compare November 12, 2025 03:37
@henryiii
Copy link
Contributor Author

henryiii commented Nov 12, 2025

New version, ignoring this rule in tests, but still applying it to the one clear dict case. Is that better? (I can't apply a rule like disabling it for dict on only tests/, AFAIK.)

@henryiii henryiii force-pushed the henryiii/chore/ruff-C4 branch from 54cff86 to e5a48c5 Compare November 12, 2025 18:39
@notatallshaw
Copy link
Member

notatallshaw commented Nov 13, 2025

I would strongly prefer setting allowing dict calls with keyword arguments as opposed to excluding C408 arbitrarily on tests, and leave the correct syntax up to authors and reviewers, i.e.

[tool.ruff.lint.flake8-comprehensions]
allow-dict-calls-with-keyword-arguments = true

Also, to a much lesser extent, I don't prefer the transformation of the dict. But I am not going to block this PR over my personal preference.

henryiii and others added 3 commits November 13, 2025 21:40
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
@henryiii henryiii force-pushed the henryiii/chore/ruff-C4 branch from fd3824f to c2d22f4 Compare November 14, 2025 02:43
@henryiii
Copy link
Contributor Author

I'm happy to go with that if you feel strongly enough. I don't think the core library (for something like packaging) should have any dict(kw)'s, but fine to disable it. Tests don't matter, but I can't change a setting per directory.

@henryiii henryiii merged commit 013f3b0 into pypa:main Nov 14, 2025
3 checks passed
@henryiii henryiii deleted the henryiii/chore/ruff-C4 branch November 14, 2025 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants