Skip to content

Commit

Permalink
Using typing.final for 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorpela committed Oct 14, 2022
1 parent e4409f2 commit d60b95a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
6 changes: 5 additions & 1 deletion overrides/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from overrides.enforce import EnforceOverrides
from overrides.final import final
import sys
if sys.version_info < (3, 11):
from overrides.final import final
else:
from typing import final
from overrides.overrides import __VERSION__, overrides

__all__ = [
Expand Down
4 changes: 2 additions & 2 deletions overrides/enforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def _check_if_overrides_without_overrides_decorator(name, value, bases):
def _check_if_overrides_final_method(name, bases):
for base in bases:
base_class_method = getattr(base, name, False)
# `__finalized__` is added by `@final` decorator
if getattr(base_class_method, "__finalized__", False):
# `__final__` is added by `@final` decorator
if getattr(base_class_method, "__final__", False):
raise TypeError(
f"Method {name} is finalized in {base}, it cannot be overridden"
)
Expand Down
5 changes: 1 addition & 4 deletions overrides/final.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
from types import FunctionType
from typing import Callable, TypeVar, Union

__VERSION__ = "0.1"


_WrappedMethod = TypeVar("_WrappedMethod", bound=Union[FunctionType, Callable])


Expand All @@ -44,5 +41,5 @@ def method(self): #causes an error
:raises AssertionError: if there exists a match in sub classes for the method name
:return: method
"""
setattr(method, "__finalized__", True)
setattr(method, "__final__", True)
return method
6 changes: 2 additions & 4 deletions overrides/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,8 @@ def _validate_method(method, super_class, check_signature):
is_static = isinstance(
inspect.getattr_static(super_class, method.__name__), staticmethod
)
if hasattr(super_method, "__finalized__"):
finalized = getattr(super_method, "__finalized__")
if finalized:
raise TypeError(f"{method.__name__}: is finalized in {super_class}")
if getattr(super_method, "__final__", False):
raise TypeError(f"{method.__name__}: is finalized in {super_class}")
if not method.__doc__:
method.__doc__ = super_method.__doc__
if (
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from os.path import abspath, join, dirname

name = "Mikko Korpela"
# I might be just a little bit too much afraid of those bots..
# I might be just a bit too much afraid of those bots.
address = name.lower().replace(" ", ".") + chr(64) + "gmail.com"

desc = "A decorator to automatically detect mismatch when overriding a method."
Expand Down

0 comments on commit d60b95a

Please sign in to comment.