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

Handle common cases of mutable default arguments explicitly #2651

Conversation

eapolinario
Copy link
Collaborator

Why are the changes needed?

In #2401 we added a check against mutable default arguments where we simply confirmed that the object was hashable. This works, but it's too restrictive as it prevents other commonly used objects (e.g. dataclasses) from being used as default values.

Even sophisticated linters like ruff only detect mutable default arguments for lists, dictionaries, and sets. For example, this sample code:

from dataclasses import dataclass
from mashumaro import DataClassDictMixin

@dataclass
class DC(DataClassDictMixin):
    a: int
    b: str

def f(dc: DC = DC(42, "43"), xs: list[int] = [1,2], d: dict[str, int] = {"abc": 42}, s: set(int) = set([1,2,3])):
    ...

returns this error in ruff:

❯ ruff check example
example/dc.py:9:46: B006 Do not use mutable data structures for argument defaults
   |
 7 |     b: str
 8 |
 9 | def f(dc: DC = DC(42, "43"), xs: list[int] = [1,2], d: dict[str, int] = {"abc": 42}, s: set(int) = set([1,2,3])):
   |                                              ^^^^^ B006
10 |     ...
   |
   = help: Replace with `None`; initialize within function

example/dc.py:9:73: B006 Do not use mutable data structures for argument defaults
   |
 7 |     b: str
 8 |
 9 | def f(dc: DC = DC(42, "43"), xs: list[int] = [1,2], d: dict[str, int] = {"abc": 42}, s: set(int) = set([1,2,3])):
   |                                                                         ^^^^^^^^^^^ B006
10 |     ...
   |
   = help: Replace with `None`; initialize within function

example/dc.py:9:100: B006 Do not use mutable data structures for argument defaults
   |
 7 |     b: str
 8 |
 9 | def f(dc: DC = DC(42, "43"), xs: list[int] = [1,2], d: dict[str, int] = {"abc": 42}, s: set(int) = set([1,2,3])):
   |                                                                                                    ^^^^^^^^^^^^ B006
10 |     ...
   |
   = help: Replace with `None`; initialize within function

Found 3 errors.
No fixes available (3 hidden fixes can be enabled with the `--unsafe-fixes` option).

Notice how the dataclass is not classified as a mutable default arg.

What changes were proposed in this pull request?

We handle the 3 common cases explicitly instead of checking if the object is hashable.

How was this patch tested?

unit tests.

Setup process

Screenshots

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

Related PRs

Docs link

@eapolinario eapolinario merged commit 7d1227b into master Aug 6, 2024
97 checks passed
mao3267 pushed a commit to mao3267/flytekit that referenced this pull request Aug 9, 2024
…#2651)

Signed-off-by: Eduardo Apolinario <[email protected]>
Co-authored-by: Eduardo Apolinario <[email protected]>
Signed-off-by: mao3267 <[email protected]>
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