mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Allow self binding for generic ParamSpec

Open cdce8p opened this issue 1 year ago • 4 comments

Required for https://github.com/python/typeshed/pull/11662 Discussion: https://discuss.python.org/t/allow-self-binding-for-generic-paramspec/50948

cdce8p avatar Apr 14 '24 13:04 cdce8p

Diff from mypy_primer, showing the effect of this PR on open source code:

antidote (https://github.com/Finistere/antidote)
+ tests/test_examples.py:37: error: Invalid self argument "Files" to attribute function "read" with type "Callable[[str], None]"  [misc]
+ tests/test_examples.py:37: error: Too many arguments  [call-arg]
+ tests/test_examples.py:41: error: Invalid self argument "Files" to attribute function "read" with type "Callable[[str], None]"  [misc]
+ tests/test_examples.py:41: error: Too many arguments  [call-arg]
+ tests/core/test_inject.py:517: error: Attribute function "method" with type "Callable[[], None]" does not accept self argument  [misc]
+ tests/core/test_inject.py:519: error: Invalid self argument "Conf" to attribute function "get" with type "Callable[[str], None]"  [misc]
+ tests/core/test_inject.py:519: error: Too many arguments  [call-arg]
+ tests/core/test_inject.py:522: error: Attribute function "method" with type "Callable[[], None]" does not accept self argument  [misc]
+ tests/core/test_inject.py:523: error: Invalid self argument "Conf" to attribute function "get" with type "Callable[[str], None]"  [misc]
+ tests/core/test_inject.py:523: error: Unexpected keyword argument "key"  [call-arg]

werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/test.py:370: error: Too many positional arguments  [misc]
+ src/werkzeug/test.py:370: error: Argument 1 has incompatible type "Mapping[str, Any]"; expected "bool"  [arg-type]

ibis (https://github.com/ibis-project/ibis)
- ibis/expr/types/relations.py:3064: error: Missing positional argument "predicates" in call to "__call__" of "_Wrapped"  [call-arg]

github-actions[bot] avatar Apr 14 '24 13:04 github-actions[bot]

Primer result analysis:

antidote

They use a custom dependency injection and function overload to remove the first argument with Concatenate. https://github.com/Finistere/antidote/blob/v2.0.0/src/antidote/core/init.py#L1201-L1204 While also freely mixing calling the function with self or the class name. https://github.com/Finistere/antidote/blob/v2.0.0/tests/test_examples.py#L36-L44

werkzeug

The error comes down to this

class A:
    import json
    json_dumps = staticmethod(json.dumps)

    def func(self, j: str) -> None:
        self.json_dumps(j)

Mypy doesn't recognize that json_dumps is a @staticmethod. A workaround would be to move to the module scope and remove the staticmethod wrapper.

ibis

False positive which is removed.

cdce8p avatar Apr 14 '24 13:04 cdce8p

Diff from mypy_primer, showing the effect of this PR on open source code:

ibis (https://github.com/ibis-project/ibis)
- ibis/expr/types/relations.py:3251: error: Missing positional argument "predicates" in call to "__call__" of "_Wrapped"  [call-arg]

xarray (https://github.com/pydata/xarray)
+ xarray/tests/test_formatting_html.py: note: In member "test_empty_mapping" of class "Test_summarize_datatree_children":
+ xarray/tests/test_formatting_html.py:268: error: Invalid self argument "Test_summarize_datatree_children" to attribute function "func" with type "Callable[[Mapping[str, DataTree[Any]]], None]"  [misc]
+ xarray/tests/test_formatting_html.py:268: error: Too many arguments  [call-arg]
+ xarray/tests/test_formatting_html.py: note: In member "test_one_child" of class "Test_summarize_datatree_children":
+ xarray/tests/test_formatting_html.py:289: error: Invalid self argument "Test_summarize_datatree_children" to attribute function "func" with type "Callable[[Mapping[str, DataTree[Any]]], None]"  [misc]
+ xarray/tests/test_formatting_html.py:289: error: Too many arguments  [call-arg]
+ xarray/tests/test_formatting_html.py: note: In member "test_two_children" of class "Test_summarize_datatree_children":
+ xarray/tests/test_formatting_html.py:316: error: Invalid self argument "Test_summarize_datatree_children" to attribute function "func" with type "Callable[[Mapping[str, DataTree[Any]]], None]"  [misc]
+ xarray/tests/test_formatting_html.py:316: error: Too many arguments  [call-arg]
+ xarray/tests/test_formatting_html.py: note: In member "test_end" of class "Test__wrap_datatree_repr":
+ xarray/tests/test_formatting_html.py:335: error: Invalid self argument "Test__wrap_datatree_repr" to attribute function "func" with type "Callable[[str, bool], None]"  [misc]
+ xarray/tests/test_formatting_html.py:335: error: Function gets multiple values for keyword argument "end"  [misc]
+ xarray/tests/test_formatting_html.py: note: In member "test_not_end" of class "Test__wrap_datatree_repr":
+ xarray/tests/test_formatting_html.py:367: error: Invalid self argument "Test__wrap_datatree_repr" to attribute function "func" with type "Callable[[str, bool], None]"  [misc]
+ xarray/tests/test_formatting_html.py:367: error: Function gets multiple values for keyword argument "end"  [misc]
- xarray/tests/test_plot.py:226: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
- xarray/tests/test_plot.py:229: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
- xarray/tests/test_plot.py:232: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
+ xarray/tests/test_plot.py:226: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
+ xarray/tests/test_plot.py:229: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
+ xarray/tests/test_plot.py:232: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
- xarray/tests/test_plot.py:235: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
+ xarray/tests/test_plot.py:235: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
- xarray/tests/test_plot.py:245: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
- xarray/tests/test_plot.py:248: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
- xarray/tests/test_plot.py:252: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
- xarray/tests/test_plot.py:255: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
+ xarray/tests/test_plot.py:245: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
+ xarray/tests/test_plot.py:248: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
+ xarray/tests/test_plot.py:252: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
+ xarray/tests/test_plot.py:255: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
- xarray/tests/test_plot.py:266: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
- xarray/tests/test_plot.py:270: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
+ xarray/tests/test_plot.py:266: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
+ xarray/tests/test_plot.py:270: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
- xarray/tests/test_plot.py:304: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
- xarray/tests/test_plot.py:307: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
+ xarray/tests/test_plot.py:304: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
+ xarray/tests/test_plot.py:307: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
- xarray/tests/test_plot.py:317: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
+ xarray/tests/test_plot.py:317: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
- xarray/tests/test_plot.py:389: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
+ xarray/tests/test_plot.py:389: error: Invalid self argument "DataArrayPlotAccessor" to attribute function "__call__" with type "Callable[[DataArray, DefaultNamedArg(Hashable | None, 'row'), DefaultNamedArg(Hashable | None, 'col'), DefaultNamedArg(int | None, 'col_wrap'), DefaultNamedArg(Any | None, 'ax'), DefaultNamedArg(Hashable | None, 'hue'), DefaultNamedArg(dict[str, Any] | None, 'subplot_kws'), KwArg(Any)], None]"  [misc]
- xarray/tests/test_plot.py:519: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]

... (truncated 117 lines) ...

antidote (https://github.com/Finistere/antidote)
+ tests/test_examples.py:37: error: Invalid self argument "Files" to attribute function "read" with type "Callable[[str], None]"  [misc]
+ tests/test_examples.py:37: error: Too many arguments  [call-arg]
+ tests/test_examples.py:41: error: Invalid self argument "Files" to attribute function "read" with type "Callable[[str], None]"  [misc]
+ tests/test_examples.py:41: error: Too many arguments  [call-arg]
+ tests/core/test_inject.py:517: error: Attribute function "method" with type "Callable[[], None]" does not accept self argument  [misc]
+ tests/core/test_inject.py:519: error: Invalid self argument "Conf" to attribute function "get" with type "Callable[[str], None]"  [misc]
+ tests/core/test_inject.py:519: error: Too many arguments  [call-arg]
+ tests/core/test_inject.py:522: error: Attribute function "method" with type "Callable[[], None]" does not accept self argument  [misc]
+ tests/core/test_inject.py:523: error: Invalid self argument "Conf" to attribute function "get" with type "Callable[[str], None]"  [misc]
+ tests/core/test_inject.py:523: error: Unexpected keyword argument "key"  [call-arg]

werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/test.py:370: error: Too many positional arguments  [misc]
+ src/werkzeug/test.py:370: error: Argument 1 has incompatible type "Mapping[str, Any]"; expected "bool"  [arg-type]

github-actions[bot] avatar Jul 03 '24 08:07 github-actions[bot]