Skip inapplicable parent overloads during override checking (#2880)#2880
Open
migeed-z wants to merge 2 commits intofacebook:mainfrom
Open
Skip inapplicable parent overloads during override checking (#2880)#2880migeed-z wants to merge 2 commits intofacebook:mainfrom
migeed-z wants to merge 2 commits intofacebook:mainfrom
Conversation
Differential Revision: D97799708
This comment has been minimized.
This comment has been minimized.
migeed-z
added a commit
to migeed-z/pyrefly
that referenced
this pull request
Mar 23, 2026
…#2880) Summary: When binding an overloaded method to an object, filter out overloads whose `self` parameter type is incompatible with the object type. This fixes false positive `bad-override` errors when subclassing `str` (and similar classes with `LiteralString` overloads) and adding optional parameters to overridden methods. For example, `str.upper()` has two overloads: 1. `(self: LiteralString) -> LiteralString` 2. `(self) -> str` When `MyString(str)` overrides `upper()`, the override check previously required the child to satisfy ALL parent overloads, including the `LiteralString` one. Since `MyString` is not `LiteralString`, the check failed with a false positive. Now, inapplicable overloads are filtered out during method binding. Fixes facebook#2309 Differential Revision: D97805178
bcb4aec to
c36cf36
Compare
…#2880) Summary: Pull Request resolved: facebook#2880 When binding an overloaded method to an object, filter out overloads whose `self` parameter type is incompatible with the object type. This fixes false positive `bad-override` errors when subclassing `str` (and similar classes with `LiteralString` overloads) and adding optional parameters to overridden methods. For example, `str.upper()` has two overloads: 1. `(self: LiteralString) -> LiteralString` 2. `(self) -> str` When `MyString(str)` overrides `upper()`, the override check previously required the child to satisfy ALL parent overloads, including the `LiteralString` one. Since `MyString` is not `LiteralString`, the check failed with a false positive. Now, inapplicable overloads are filtered out during method binding. Fixes facebook#2309 Differential Revision: D97805178
c36cf36 to
c25e72e
Compare
This comment has been minimized.
This comment has been minimized.
|
Diff from mypy_primer, showing the effect of this PR on open source code: dulwich (https://github.com/dulwich/dulwich)
- ERROR dulwich/cli.py:801:9-14: Class member `Pager.write` overrides parent class `TextIO` in an inconsistent manner [bad-override]
- ERROR dulwich/cli.py:867:9-19: Class member `Pager.writelines` overrides parent class `TextIO` in an inconsistent manner [bad-override]
setuptools (https://github.com/pypa/setuptools)
- ERROR setuptools/_vendor/importlib_metadata/_text.py:97:9-14: Class member `FoldedCase.split` overrides parent class `str` in an inconsistent manner [bad-override]
+ ERROR setuptools/_vendor/importlib_metadata/_text.py:97:9-14: Class member `FoldedCase.split` overrides parent class `str` in an inconsistent manner [bad-param-name-override]
- ERROR setuptools/_vendor/jaraco/text/__init__.py:132:9-14: Class member `FoldedCase.split` overrides parent class `str` in an inconsistent manner [bad-override]
+ ERROR setuptools/_vendor/jaraco/text/__init__.py:132:9-14: Class member `FoldedCase.split` overrides parent class `str` in an inconsistent manner [bad-param-name-override]
- ERROR setuptools/_vendor/jaraco/text/__init__.py:431:9-17: Class member `SeparatedValues.__iter__` overrides parent class `str` in an inconsistent manner [bad-override]
django-stubs (https://github.com/typeddjango/django-stubs)
- ERROR django-stubs/utils/safestring.pyi:14:9-16: Class member `SafeString.__add__` overrides parent class `str` in an inconsistent manner [bad-override]
bidict (https://github.com/jab/bidict)
- ERROR bidict/_bidict.py:158:9-15: Class member `MutableBidict.update` overrides parent class `MutableBidirectionalMapping` in an inconsistent manner [bad-override]
pip (https://github.com/pypa/pip)
- ERROR src/pip/_vendor/rich/_null_file.py:39:9-19: Class member `NullFile.writelines` overrides parent class `IO` in an inconsistent manner [bad-override]
- ERROR src/pip/_vendor/rich/_null_file.py:59:9-14: Class member `NullFile.write` overrides parent class `IO` in an inconsistent manner [bad-override]
mypy (https://github.com/python/mypy)
- ERROR mypy/dmypy_util.py:107:9-14: Class member `WriteToConn.write` overrides parent class `TextIO` in an inconsistent manner [bad-override]
- ERROR mypy/dmypy_util.py:115:9-19: Class member `WriteToConn.writelines` overrides parent class `TextIO` in an inconsistent manner [bad-override]
- ERROR mypy/typeshed/stdlib/codecs.pyi:302:9-19: Class member `StreamReaderWriter.writelines` overrides parent class `TextIO` in an inconsistent manner [bad-override]
- ERROR mypy/typeshed/stdlib/urllib/response.pyi:21:9-14: Class member `addbase.write` overrides parent class `_TemporaryFileWrapper` in an inconsistent manner [bad-override]
- ERROR mypy/typeshed/stdlib/urllib/response.pyi:22:9-19: Class member `addbase.writelines` overrides parent class `_TemporaryFileWrapper` in an inconsistent manner [bad-override]
- ERROR mypy/typeshed/stdlib/weakref.pyi:121:9-15: Class member `WeakValueDictionary.update` overrides parent class `MutableMapping` in an inconsistent manner [bad-override]
- ERROR mypy/typeshed/stdlib/weakref.pyi:177:9-15: Class member `WeakKeyDictionary.update` overrides parent class `MutableMapping` in an inconsistent manner [bad-override]
pyodide (https://github.com/pyodide/pyodide)
- ERROR src/py/_pyodide/_core_docs.py:1180:9-15: Class member `JsMutableMap.update` overrides parent class `MutableMapping` in an inconsistent manner [bad-override]
rich (https://github.com/Textualize/rich)
- ERROR rich/_null_file.py:39:9-19: Class member `NullFile.writelines` overrides parent class `IO` in an inconsistent manner [bad-override]
- ERROR rich/_null_file.py:59:9-14: Class member `NullFile.write` overrides parent class `IO` in an inconsistent manner [bad-override]
spark (https://github.com/apache/spark)
- ERROR python/pyspark/logger/worker_io.py:90:9-14: Class member `DelegatingTextIOWrapper.write` overrides parent class `TextIO` in an inconsistent manner [bad-override]
- ERROR python/pyspark/logger/worker_io.py:93:9-19: Class member `DelegatingTextIOWrapper.writelines` overrides parent class `TextIO` in an inconsistent manner [bad-override]
schema_salad (https://github.com/common-workflow-language/schema_salad)
- ERROR mypy-stubs/rdflib/namespace/__init__.pyi:38:9-20: Class member `Namespace.__getitem__` overrides parent class `str` in an inconsistent manner [bad-override]
pytest (https://github.com/pytest-dev/pytest)
- ERROR src/_pytest/capture.py:273:9-14: Class member `DontReadFromInput.write` overrides parent class `TextIO` in an inconsistent manner [bad-override]
- ERROR src/_pytest/capture.py:276:9-19: Class member `DontReadFromInput.writelines` overrides parent class `TextIO` in an inconsistent manner [bad-override]
cwltool (https://github.com/common-workflow-language/cwltool)
- ERROR mypy-stubs/rdflib/namespace/__init__.pyi:38:9-20: Class member `Namespace.__getitem__` overrides parent class `str` in an inconsistent manner [bad-override]
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
When binding an overloaded method to an object, filter out overloads whose
selfparameter type is incompatible with the object type. This fixes falsepositive
bad-overrideerrors when subclassingstr(and similar classeswith
LiteralStringoverloads) and adding optional parameters to overriddenmethods.
For example,
str.upper()has two overloads:(self: LiteralString) -> LiteralString(self) -> strWhen
MyString(str)overridesupper(), the override check previouslyrequired the child to satisfy ALL parent overloads, including the
LiteralStringone. SinceMyStringis notLiteralString, the checkfailed with a false positive. Now, inapplicable overloads are filtered out
during method binding.
Fixes #2309
Differential Revision: D97805178