Skip to content

type DataFrame, Column, and GroupBy as Protocol #291

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

Merged
merged 1 commit into from
Oct 26, 2023

Conversation

MarcoGorelli
Copy link
Contributor

@MarcoGorelli MarcoGorelli commented Oct 23, 2023

technically we're defining protocols here - classes which must have certain attributes and methods

might as well type these as Protocol anyway. this will make type checking for implementations simpler, as it tells you whether anything's missing

minimal demo of how this helps:

(.venv) marcogorelli@DESKTOP-U8OKFP3:~/tmp$ cat t.py
from __future__ import annotations

from typing import Protocol
from typing_extensions import Self


class Cat(Protocol):
    def add(self, other: Self) -> Self:
        ...
    def sub(self, other: Self) -> Self:
        ...


class FancyCat(Cat):
    def __init__(self, value: int):
        self._value = value

    def add(self, other: Self) -> FancyCat:
        return FancyCat(self._value + other._value)

(.venv) marcogorelli@DESKTOP-U8OKFP3:~/tmp$ mypy t.py
t.py:19: error: Cannot instantiate abstract class "FancyCat" with abstract attribute "sub"  [abstract]
Found 1 error in 1 file (checked 1 source file)

"oh, right, I didn't implement sub. let me add that"

+    def sub(self, other: Self) -> FancyCat:
+        return FancyCat(self._value - other._value)
$ mypy t.py
Success: no issues found in 1 source file

@MarcoGorelli MarcoGorelli marked this pull request as ready for review October 23, 2023 14:56
@MarcoGorelli MarcoGorelli added the static typing type annotations, use of type checkers directly from the spec label Oct 23, 2023
@MarcoGorelli
Copy link
Contributor Author

nobody's objecting, and type checking doesn't completely work properly without this, so let's merge

@MarcoGorelli MarcoGorelli merged commit 37ac1f0 into data-apis:main Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
static typing type annotations, use of type checkers directly from the spec
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant