fix invalid-argument "Expected first argument to super" false positives #3065#3069
fix invalid-argument "Expected first argument to super" false positives #3065#3069asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
Conversation
|
Diff from mypy_primer, showing the effect of this PR on open source code: setuptools (https://github.com/pypa/setuptools)
- ERROR setuptools/_distutils/dir_util.py:28:9-33: Expected first argument to `super` to be a class object, got `type[Self@SkipRepeatAbsolutePaths]` [invalid-argument]
comtypes (https://github.com/enthought/comtypes)
- ERROR comtypes/safearray.py:393:13-51: Expected first argument to `super` to be a class object, got `type[_Pointer[_Pointer[Unknown]]]` [invalid-argument]
+ ERROR comtypes/safearray.py:393:13-51: Illegal `super(type[_Pointer[_Pointer[Unknown]]], _make_safearray_type.__)` call: `_make_safearray_type.__` is not an instance or subclass of `type[_Pointer[_Pointer[Unknown]]]` [invalid-super-call]
dd-trace-py (https://github.com/DataDog/dd-trace-py)
- ERROR ddtrace/internal/settings/_config.py:825:20-47: Expected first argument to `super` to be a class object, got `type[Self@Config]` [invalid-argument]
- ERROR ddtrace/internal/settings/_config.py:830:20-47: Expected first argument to `super` to be a class object, got `type[Self@Config]` [invalid-argument]
zope.interface (https://github.com/zopefoundation/zope.interface)
- ERROR src/zope/interface/ro.py:209:16-38: Expected first argument to `super` to be a class object, got `type[Self@_NamedBool]` [invalid-argument]
+ ERROR src/zope/interface/ro.py:210:9-22: Object of class `_NamedBool` has no attribute `__name__` [missing-attribute]
prefect (https://github.com/PrefectHQ/prefect)
- ERROR src/integrations/prefect-dbt/prefect_dbt/cli/commands.py:422:16-48: Expected first argument to `super` to be a class object, got `type[Self@DbtCoreOperation]` [invalid-argument]
- ERROR src/integrations/prefect-dbt/prefect_dbt/cli/commands.py:437:16-48: Expected first argument to `super` to be a class object, got `type[Self@DbtCoreOperation]` [invalid-argument]
|
Primer Diff Classification✅ 5 improvement(s) | 5 project(s) total | +2, -7 errors 5 improvement(s) across setuptools, comtypes, dd-trace-py, zope.interface, prefect.
Detailed analysis✅ Improvement (5)setuptools (-1)
comtypes (+1, -1)
dd-trace-py (-2)
zope.interface (+1, -1)
The new Net assessment: removing 1 clear false positive and adding 1 debatable error (co-reported by pyright) is a net improvement. The core fix (handling
prefect (-2)
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (5 LLM) |
There was a problem hiding this comment.
Pull request overview
Fixes false-positive invalid-argument diagnostics for super(cls, obj) when the first argument is a dynamically-computed class object (e.g. self.__class__, type(self), or cls in __new__), aligning behavior with other type checkers and addressing #3065.
Changes:
- Extend
superexplicit-args handling to accepttype[ClassType]/type[SelfType]as a valid first argument tosuper. - Add a regression test covering multiple dynamic-first-arg
super(...)patterns from the reported repros.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
pyrefly/lib/alt/solve.rs |
Accepts additional type shapes for super’s first argument to avoid invalid-argument false positives. |
pyrefly/lib/test/class_super.rs |
Adds a regression testcase ensuring these dynamic-first-arg super(...) calls do not error. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| let cls_type = self.get_idx(*cls_binding).ty().clone(); | ||
| let cls = match &cls_type { | ||
| Type::Any(style) => return style.propagate(), | ||
| Type::ClassDef(cls) => cls, | ||
| Type::Type(box Type::ClassType(cls) | box Type::SelfType(cls)) => { | ||
| cls.class_object() | ||
| } |
There was a problem hiding this comment.
cls_type is cloned unconditionally (let cls_type = ... .clone()), even though it’s only needed for display in the error path. Consider keeping a &Type (e.g. let cls_type = self.get_idx(*cls_binding).ty();) and cloning only when formatting the error message to avoid extra work on every super(...) solve.
Summary
Fixes #3065
relaxing the explicit super(cls, obj) first-argument check.
now accepts not just literal class defs, but also class-object values typed as type[C] and type[Self], which covers patterns like
self.__class__,type(self), and cls in__new__.Test Plan
add test