Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-bugbear] Do not raise error if keyword argument is present and target-python version is less or equals than 3.9 (B903) #15549

Merged
merged 1 commit into from
Jan 17, 2025

Conversation

guillaumeLepape
Copy link
Contributor

[flake8-bugbear] Do not raise error if keyword argument is present and target-python version is less or equals than 3.9 (B903)

Summary

I have this piece of code in one of my repo

class RuleMetadata:
    def __init__(
        self,
        *,
        function: Union[
            Callable[[Session, UserModel, RuleComputeFinaleFromGroupRank], None],
            Callable[[Session, UserModel, RuleComputePoints], None],
        ],
        attribute: str,
        required_admin: bool = False,
    ) -> None:
        self.function = function
        self.attribute = attribute
        self.required_admin = required_admin

When I did this, I started by using dataclass, although I wanted the 3 arguments of the init function to be keywords only. As dataclass does not support kw_only before python 3.10 (reference https://docs.python.org/3/library/dataclasses.html#module-contents), I ended up writing the init function myself.

With the current implementation of B903, this will raise an error. My suggestion would be not to raise error if an keyword only argument and target-version<=3.9.

Related github issue : #15548

Test Plan

I added a new usecase in class_as_data_structure.py, a class with keyword only.
I tested this file with the target-version = latest python and with python3.9.
With the latest python, I'm expecting error and python3.9 no error should be raised.

…nt is present and target-python version is less or equals than 3.9
@guillaumeLepape guillaumeLepape changed the title \[flake8-bugbear\] Do not raise error if keyword argument is present and target-python version is less or equals than 3.9 (B903) [flake8-bugbear] Do not raise error if keyword argument is present and target-python version is less or equals than 3.9 (B903) Jan 17, 2025
Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -1 violations, +0 -0 fixes in 1 projects; 54 projects unchanged)

apache/airflow (+0 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

- providers/src/airflow/providers/google/cloud/operators/dataflow.py:64:1: B903 Class could be dataclass or namedtuple

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
B903 1 0 1 0 0

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Jan 17, 2025
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, and the ecosystem check confirms that this resolves a false positive. Thank you so much!

@MichaReiser MichaReiser merged commit 4fdf8af into astral-sh:main Jan 17, 2025
21 checks passed
@guillaumeLepape guillaumeLepape deleted the b903_suggestion branch January 17, 2025 11:48
dcreager added a commit that referenced this pull request Jan 17, 2025
* main:
  [red-knot] Inline `SubclassOfType::as_instance_type_of_metaclass()` (#15556)
  [`flake8-comprehensions`] strip parentheses around generators in `unnecessary-generator-set` (`C401`) (#15553)
  [`pylint`] Implement `redefined-slots-in-subclass` (`W0244`) (#9640)
  [`flake8-bugbear`] Do not raise error if keyword argument is present and target-python version is less or equals than 3.9 (`B903`) (#15549)
  [red-knot] `type[T]` is disjoint from `type[S]` if the metaclass of `T` is disjoint from the metaclass of `S` (#15547)
  [red-knot] Pure instance variables declared in class body (#15515)
  Update snapshots of #15507 with new annotated snipetts rendering (#15546)
  [`pylint`] Do not report methods with only one `EM101`-compatible `raise` (`PLR6301`) (#15507)
  Fix unstable f-string formatting for expressions containing a trailing comma (#15545)
  Support `knot.toml` files in project discovery (#15505)
  Add support for configuring knot in `pyproject.toml` files (#15493)
  Fix bracket spacing for single-element tuples in f-string expressions (#15537)
  [`flake8-simplify`] Do not emit diagnostics for expressions inside string type annotations (`SIM222`, `SIM223`) (#15405)
  [`flake8-pytest-style`] Do not emit diagnostics for empty `for` loops (`PT012`, `PT031`) (#15542)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants