Skip to content

Comments

Fix function supports_feature to return False when target_versions is empty. [skip news]#4726

Merged
tusharsadhwani merged 2 commits intopsf:mainfrom
ranjodhsingh1729:fix-supports-feature
Aug 4, 2025
Merged

Fix function supports_feature to return False when target_versions is empty. [skip news]#4726
tusharsadhwani merged 2 commits intopsf:mainfrom
ranjodhsingh1729:fix-supports-feature

Conversation

@ranjodhsingh1729
Copy link
Contributor

@ranjodhsingh1729 ranjodhsingh1729 commented Aug 4, 2025

Description

def supports_feature(target_versions: set[TargetVersion], feature: Feature) -> bool:
    return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)

Here when target_versions is empty, expression gets reduced to all([]) which evaluates to True which is not the correct behaviour.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the
    stability policy?
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

@tusharsadhwani
Copy link
Collaborator

Can you expand on a use case where this bites a user? Do we actually end up passing an empty list to supports_feature in some case?

@ranjodhsingh1729
Copy link
Contributor Author

Can you expand on a use case where this bites a user? Do we actually end up passing an empty list to supports_feature in some case?

No, this doesn't affect functionality in any way.
There are only two places where this function is currently used:

  1. In get_grammars, which simply returns all grammars if target_versions is empty - no call to supports_feature is made.
  2. In _format_str_once, where supports_feature is called with versions obtained from detect_target_versions. detect_target_versions cannot possibly return an empty set (if it does that would indicate some issue elsewhere), so again, no issue here.

The misleading behavior of this function ended up wasting quite a bit of my time while working on #4720, so I figured it’s worth fixing to save others the trouble down the line.

@tusharsadhwani
Copy link
Collaborator

If the function is never called with an empty value, and is never intended to be called with an empty value, let's throw a meaningful error instead of returning False.

P.S. The CI failure does seem unrelated.

@tusharsadhwani tusharsadhwani added the skip news Pull requests that don't need a changelog entry. label Aug 4, 2025
@tusharsadhwani tusharsadhwani merged commit 3a96e06 into psf:main Aug 4, 2025
39 of 40 checks passed
ranjodhsingh1729 added a commit to ranjodhsingh1729/black that referenced this pull request Aug 4, 2025
…` is empty. [skip news] (psf#4726)

* Fix function `supports_feature` to return False when `target_versions` is empty.

* supports_feature: raise ValueError if target_versions is empty
cobaltt7 pushed a commit that referenced this pull request Aug 19, 2025
…xcept*` without `as`. (#4720)

* Remove parentheses around multiple exception types in `except` and `except*` when not using the `as` clause. (#4678)

* Add Changelog Entry

* Add Unparenthesized Except Tuple Detection.

* Add tests for `except*`.

* Oops! Wrong Version.

* Fix function `supports_feature` to return False when `target_versions`  is empty. [skip news] (#4726)

* Fix function `supports_feature` to return False when `target_versions` is empty.

* supports_feature: raise ValueError if target_versions is empty

* Simplify conditional logic for removing except type parens.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news Pull requests that don't need a changelog entry.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants