Skip to content

Handle dataclass field kw_only overrides #566

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

Closed
wants to merge 3 commits into from

Conversation

pt2302
Copy link
Contributor

@pt2302 pt2302 commented Jun 24, 2025

Description

This PR allows for field-level overrides to set kw_only=False for a dataclass when the class has kw_only set to True.

Relevant Issue

Closes #494.

Testing Instructions

To test this PR manually, create a Python script like

from dataclasses import dataclass, field

@dataclass(kw_only=True)
class SomeClass:
    x: int = field(kw_only=False)

SomeClass(x=1)
SomeClass(1)

and run pyrefly check. Verify that no errors are generated.

Note: this PR does not handle cases where there are mixed positional and keyword-only arguments; that will be handled in a follow-up PR (related issue: #493).

@facebook-github-bot
Copy link
Contributor

@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@yangdanny97
Copy link
Contributor

Nice, thanks!

field.as_param(
&name,
field_flags.is_set(&DataclassKeywords::DEFAULT),
field_flags
Copy link
Contributor

Choose a reason for hiding this comment

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

You can change the default value passed to is_set like so:

field_flags.is_set(&(DataclassKeywords::KW_ONLY.0, kw_only)),

No need for a new get_explicit method.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll address this in the internal version of the PR

@facebook-github-bot
Copy link
Contributor

@yangdanny97 merged this pull request in f3503d9.

vagabond-0 pushed a commit to vagabond-0/pyrefly that referenced this pull request Jun 27, 2025
Summary:
## Description

This PR allows for field-level overrides to set `kw_only=False` for a dataclass when the class has `kw_only` set to `True`.

## Relevant Issue
Closes facebook#494.

## Testing Instructions

To test this PR manually, create a Python script like
```
from dataclasses import dataclass, field

dataclass(kw_only=True)
class SomeClass:
    x: int = field(kw_only=False)

SomeClass(x=1)
SomeClass(1)

```
and run `pyrefly check`. Verify that no errors are generated.

Note: this PR does not handle cases where there are mixed positional and keyword-only arguments; that will be handled in a follow-up PR (related issue: facebook#493).

Pull Request resolved: facebook#566

Test Plan:
Imported from GitHub, without a `Test Plan:` line.

Rollback Plan:

Reviewed By: ndmitchell

Differential Revision: D77305257

Pulled By: yangdanny97

fbshipit-source-id: 7dcc8d49b9d25a35f4479fc5cca9c89b834f79ce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] handle dataclass field(kw_only=False)
4 participants