Skip to content

Static Type Checking: Enums#2153

Open
nathanwilliams-ct wants to merge 2 commits into
apache:masterfrom
nathanwilliams-ct:nathan/enum-static-type-checking
Open

Static Type Checking: Enums#2153
nathanwilliams-ct wants to merge 2 commits into
apache:masterfrom
nathanwilliams-ct:nathan/enum-static-type-checking

Conversation

@nathanwilliams-ct

Copy link
Copy Markdown
Contributor

Buildstream implements a custom FastEnum[1].
mypy only supports enum.Enum (and it's official variations) when it comes to doing static type checks on enums [2].
We can't subclass Enum in FastEnum to make mypy happy, because Enum doesn't allow subclassing [3].
So we end up with a bunch of str, int etc around the codebase,
instead of the appropriate Enum classes like OverlapAction or _Scope which are usually recorded separately in the doc strings.
This means we don't get proper static type checking on our Enums :( .
With stub files [4][5] we can lie to mypy about what type the Enum classes are[6],
so the type hints around the codebase are now correct be corrected.
Now we can have proper static type checking on the custom Enums, Yay!

So

class OverlapAction(FastEnum):
    ERROR: str
    WARNING: str
    IGNORE: str

gets stubbed as

from enum import Enum
class OverlapAction(Enum):
    ERROR: str
    WARNING: str
    IGNORE: str

[1] src/buildstream/types.py#L32
[2] https://mypy.readthedocs.io/en/stable/literal_types.html#enums
[3] https://docs.python.org/3/howto/enum.html#restricted-enum-subclassing
[4] https://typing.python.org/en/latest/guides/writing_stubs.html
[5] https://mypy.readthedocs.io/en/stable/stubgen.html
[6] python/mypy#3217

Buildstream implements a custom `FastEnum`[1].
mypy only supports `enum.Enum` (and it's official variations) when it comes to doing static type checks on enums [2].
We can't subclass Enum in FastEnum to make mypy happy, because Enum doesn't allow subclassing [3].
So we end up with a bunch of `str`, `int` etc around the codebase,
instead of the appropriate Enum classes like `OverlapAction` or `_Scope` which are usually recorded separately in the doc strings.
This means we don't get proper static type checking on our Enums :( .
With stub files [4][5] we can lie to mypy about what type the Enum classes are[6],
so the type hints around the codebase are now correct be corrected.
Now we can have proper static type checking on the custom Enums, Yay!

So

```
class OverlapAction(FastEnum):
    ERROR: str
    WARNING: str
    IGNORE: str
```

gets stubbed as

```
from enum import Enum
class OverlapAction(Enum):
    ERROR: str
    WARNING: str
    IGNORE: str
```

[1] src/buildstream/types.py#L32
[2] https://mypy.readthedocs.io/en/stable/literal_types.html#enums
[3] https://docs.python.org/3/howto/enum.html#restricted-enum-subclassing
[4] https://typing.python.org/en/latest/guides/writing_stubs.html
[5] https://mypy.readthedocs.io/en/stable/stubgen.html
[6] python/mypy#3217
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant