Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mortymacs/abcmeta
Python meta class and abstract method library with restrictions.
https://github.com/mortymacs/abcmeta
abc abcmeta abstractclass abstractmethod oop python python3
Last synced: 2 months ago
JSON representation
Python meta class and abstract method library with restrictions.
- Host: GitHub
- URL: https://github.com/mortymacs/abcmeta
- Owner: mortymacs
- License: other
- Created: 2021-06-28T08:24:54.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-12T07:34:09.000Z (7 months ago)
- Last Synced: 2024-10-12T02:49:34.005Z (3 months ago)
- Topics: abc, abcmeta, abstractclass, abstractmethod, oop, python, python3
- Language: Python
- Homepage: https://pypi.org/project/abcmeta/
- Size: 39.1 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# abcmeta
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mortymacs/abcmeta/python-test.yml?style=flat-square)](https://github.com/mortymacs/abcmeta/actions/workflows/python-test.yml)
[![PyPi version](https://img.shields.io/pypi/v/abcmeta?style=flat-square)](https://pypi.org/project/abcmeta)
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/abcmeta?style=flat-square)](https://pypi.python.org/pypi/abcmeta/)
[![PyPI download month](https://img.shields.io/pypi/dm/abcmeta?style=flat-square)](https://pypi.python.org/pypi/abcmeta/)Python meta class and abstract method library with restrictions.
This library provides a restricted way to validate abstract methods.
The Python's default abstract method library only validates the methods
that exist in the derived classes and nothing else.
What this library provides is apart from that validation it provides
validations over the method's signature.
All you need is to import `ABCMeta` and `abstractmethod` from this library.It works on both annotations and without annotations methods.
### Installation
You can install the package by `pip`:
```bash
$ pip install abcmeta
```> Note: abcmeta supports Python3.6+.
### Quick start
```python
from typing import Dict, Textfrom abcmeta import ABC, abstractmethod
class Base(ABC):
@abstractmethod
def method_2(self, name: Text, age: int) -> Dict[Text, Text]:
"""Abstract method."""@abstractmethod
def method_3(self, name, age):
"""Abstract method."""class Drived(Base):
def method_2(self, name: Text, age: int) -> Dict[Text, Text]:
return {"name": "test"}def method_3(self, name, age):
pass
```If you put a different signature, it will raise an error with 'diff' format with hints about what you've missed:
```python
class Drived(Base):
def method_2(self, name: Text, age: int) -> List[Text]:
return {"name": "test"}
```And it will raise:
```python
Traceback (most recent call last):
File "/Workspaces/test.py", line 41, in
class Drived(Base):
File "/usr/lib/python3.9/abc.py", line 85, in __new__
cls = super().__new__(mcls, name, bases, namespace, **kwargs)
File "/abcmeta/__init__.py", line 179, in __init_subclass__
raise AttributeError(
AttributeError: Signature of the derived method is not the same as parent class:
- method_2(self, name: str, age: int) -> Dict[str, str]
? ^ ^ -----+ method_2(self, name: str, age: int) -> List[str]
? ^ ^Derived method expected to return in 'typing.Dict[str, str]' type, but returns 'typing.List[str]'
```For different parameter names:
```python
class Drived(Base):
def method_2(self, username: Text, age: int) -> List[Text]:
return {"name": "test"}
```And it will raise:
```python
Traceback (most recent call last):
File "/Workspaces/test.py", line 41, in
class Drived(Base):
File "/usr/lib/python3.9/abc.py", line 85, in __new__
cls = super().__new__(mcls, name, bases, namespace, **kwargs)
File "/abcmeta/__init__.py", line 180, in __init_subclass__
raise AttributeError(
AttributeError: Signature of the derived method is not the same as parent class:
- method_2(self, name: str, age: int) -> Dict[str, str]
+ method_2(self, username: str, age: int) -> Dict[str, str]
? ++++Derived method expected to get name paramter, but gets username
```### Issue
If you're faced with a problem, please file an [issue](https://github.com/mortymacs/abcmeta/issues/new) on Github.
### Contribute
You're always welcome to contribute to the project! Please file an issue and send your great PR.
### Support
If you find this project useful and want to support its development, consider [buying me a coffee!](https://buymeacoffee.com/mortymacs)
### License
Please read the [LICENSE](./LICENSE) file.