Skip to content

Commit

Permalink
Use Self for more accurate typing (#201)
Browse files Browse the repository at this point in the history
* Use Self for more accurate typing

* chore: Remove pyright for now
  • Loading branch information
mdomke authored May 7, 2024
1 parent c96efc5 commit ec5a827
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ authors = [
]
dependencies = [
"importlib_resources>=5.10; python_version <= '3.11'",
"typing_extensions>=4.0; python_version <= '3.11'",
"iso3166",
"pycountry",
]
Expand Down
10 changes: 7 additions & 3 deletions schwifty/bban.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from typing import Any
from typing import cast

from schwifty import common
from schwifty import exceptions
Expand All @@ -11,6 +10,11 @@
from schwifty.domain import Component


try:
from typing import Self
except ImportError:
from typing_extensions import Self

EMPTY_RANGE = (0, 0)


Expand Down Expand Up @@ -61,8 +65,8 @@ class BBAN(common.Base):
.. versionadded:: 2024.01.1
"""

def __new__(cls: type[BBAN], country_code: str, value: str, **kwargs: Any) -> BBAN:
return cast(BBAN, super().__new__(cls, value, **kwargs))
def __new__(cls: type[Self], country_code: str, value: str, **kwargs: Any) -> Self:
return super().__new__(cls, value, **kwargs)

def __init__(self, country_code: str, value: str) -> None:
self.country_code = country_code
Expand Down
8 changes: 7 additions & 1 deletion schwifty/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
from typing import Any


try:
from typing import Self
except ImportError:
from typing_extensions import Self


_clean_regex = re.compile(r"\s+")


@total_ordering
class Base(str):
def __new__(cls: type[Base], value: str, **kwargs: Any) -> Base:
def __new__(cls: type[Self], value: str, **kwargs: Any) -> Self:
return super().__new__(cls, clean(value))

def __repr__(self) -> str:
Expand Down

0 comments on commit ec5a827

Please sign in to comment.