Skip to content

Fix dataclass/BaseModel field named "self" incorrectly flagged as duplicate (issue #3900)#3902

Closed
kz357 wants to merge 2 commits into
facebook:mainfrom
kz357:fix/issue-3900-pydantic-dataclass-incorrect-flag
Closed

Fix dataclass/BaseModel field named "self" incorrectly flagged as duplicate (issue #3900)#3902
kz357 wants to merge 2 commits into
facebook:mainfrom
kz357:fix/issue-3900-pydantic-dataclass-incorrect-flag

Conversation

@kz357

@kz357 kz357 commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Summary

A "@DataClass" (or Pydantic "BaseModel") field named "self" was incorrectly flagged with "bad-keyword-argument" and "bad-argument-type" errors on construction, e.g. "MyDataClass(self="test")", even though this is valid Python that runs fine.

Instance receiver is bound by prepending it as a positional argument rather than dropping the param so two params named self caused the keyword "self=" to bind to the instance param instead of the field.

Fix is to mirror CPython dataclasses as follows: when there exists a field named "self", name the instance param "dataclass_self" instead. Pydantic BaseModel also covered by this change since its init goes through get_dataclass_init.

Fixes issue #3900 + dillydill123's comment about Pydantic BaseModel

Test Plan

Added two regression tests, one for dataclass and one for BaseModel, both passing.

no diffs!

A dataclass field named `self` collided with the implicit `self` parameter of the synthesized `__init__`, producing false bad-keyword- argument and bad-argument-type errors on "Cls(self=...)".

Instance receiver is bound by prepending it as a positional argument rather than dropping the param so two params named `self` caused the keyword "self=" to bind to the instance param instead of the field.

Fix is to mirror CPython dataclasses as follows: when there exists a field named "self", name the instance param "dataclass_self__" instead. Pydantic BaseModel also covered by this change since its init goes through get_dataclass_init

Fixes issue facebook#3900 + dillydill123's comment about Pydantic BaseModel
@meta-cla

meta-cla Bot commented Jun 23, 2026

Copy link
Copy Markdown

Hi @kz357!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@kz357

kz357 commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

force-with-lease pushed user email change to mine, because I think meta CLA needs it to be my email (?)

@meta-cla meta-cla Bot added the cla signed label Jun 23, 2026
@meta-cla

meta-cla Bot commented Jun 23, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Comment thread pyrefly/lib/alt/class/dataclass.rs

@yangdanny97 yangdanny97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, I'll let @NathanTempest merge

@github-actions github-actions Bot added size/s and removed size/s labels Jun 23, 2026

@NathanTempest NathanTempest left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the great fix, @kz357 and welcome aboard!. LGTM. One micro-nit: the new comment is a doc comment but it's inside a function body which is only valid before items, so it'll trigger an unused_doc_comment warning. I will import this and change it myself as this is a pretty small fix.

@meta-codesync

meta-codesync Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

@NathanTempest has imported this pull request. If you are a Meta employee, you can view this in D109467138.

@rchen152 rchen152 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@meta-codesync meta-codesync Bot closed this in d781e76 Jun 23, 2026
@meta-codesync

meta-codesync Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

@NathanTempest merged this pull request in d781e76.

@meta-codesync meta-codesync Bot added the Merged label Jun 23, 2026
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.

4 participants