Skip to content
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

Allow self binding for generic ParamSpec #17123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cdce8p
Copy link
Collaborator

@cdce8p cdce8p commented Apr 14, 2024

This comment has been minimized.

@cdce8p
Copy link
Collaborator Author

cdce8p commented Apr 14, 2024

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 cdce8p added the topic-paramspec PEP 612, ParamSpec, Concatenate label Apr 14, 2024
@cdce8p cdce8p force-pushed the paramspec-generic-self-binding branch from fd4a62a to 83f3482 Compare July 3, 2024 08:36
Copy link
Contributor

github-actions bot commented Jul 3, 2024

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]

@Galtozzy
Copy link

Galtozzy commented Oct 8, 2024

Great work!

Do you think we can get any reviews on this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants