Skip to content

Commit

Permalink
Merge pull request #5 from curegit/develop
Browse files Browse the repository at this point in the history
Update to 0.3.0
  • Loading branch information
curegit authored Nov 7, 2023
2 parents 0516394 + f60447d commit fe3bea2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 40 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ print(xs.targ) # <class 'int'>

- Python >= 3.12

This library is written in pure Python and does not require any non-builtin modules.
This library is written in pure Python and does not require any external modules.

## Install

Expand All @@ -29,32 +29,36 @@ pip3 install reification

## API

The public API is defined under the root of the `reification` package.

### `Reified` (class)

Usage: `from reification import Reified`

`Reified` is a Mixin class designed to facilitate the creation of new types based on reified type parameters.

This class is threadsafe so that inheriting classes can be used in multiple threads.
This class is thread-safe so that inheriting classes can be used in multiple threads.

You cannot instantiate this class directly.
You cannot directly instantiate this class.

#### `targ: type | tuple[type | Any, ...] | Any` (class property)

This class property represents the type argument(s) specified for the reified generic class.
If there's more than one type argument, `targ` will be a tuple containing each given type or type-like values.
If type argument is not specified, it may return 'Any'.
If there's more than one type argument, `targ` will be a tuple containing each given type or type-like value.
If a type argument is not specified, it may return `Any`.

#### `type_args: tuple[type | Any, ...]` (class property)

This is another class property that carries the type argument(s) provided for the reified generic class.
Unlike `targ`, `type_args` always returns a tuple of the specified type arguments, even when there's only one type argument.
If no type arguments are given, it may contain single 'Any'.
If no type arguments are given, it may contain a single `Any`.

#### `__class_getitem__(cls, params: type | tuple[type | Any, ...] | Any) -> type` (special class method, for Mixin)

This method, which the class overrides, is used for creating new types each time it is called with distinct type arguments.
It serves a key role in handling parameterized generic classes, enabling the different identities on different type arguments of the same base class.

## Example Usage: Type Checked Generic Stack
## Example Usage: Type-Checked Generic Stack

```py
from reification import Reified
Expand Down Expand Up @@ -178,10 +182,6 @@ True
False
```

## Tips

`typing_inspect`

## License

[WTFPL](LICENSE)
WTFPL
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ repository = "https://github.com/curegit/reification.git"
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
packages = ["reification"]

[tool.setuptools.dynamic]
version = {attr = "reification.__version__"}
4 changes: 2 additions & 2 deletions reification/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__version__ = "0.2.0"
__version__ = "0.3.0"

from .core import Reified
from .abs import Reified

__all__ = ["Reified"]
46 changes: 46 additions & 0 deletions reification/abs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from typing import Any
from .utils import get_reified_type, tuplize_class_getitem_params


class Reified:
"""Mixin class designed to facilitate the creation of new types based on reified type parameters"""

targ: type | tuple[type | Any, ...] | Any = Any
"""
This class property represents the type argument(s) that were specified when the reified generic class was instantiated.
If there is more than one type argument, `targ` will be a tuple containing each given type.
If no type argument is specified, `Any` will be returned.
Returns:
type | tuple[type | Any, ...] | Any: The type argument(s) given when the class was instantiated.
If no type argument was given, `Any` will be returned.
"""

type_args: tuple[type | Any, ...] = (Any,)
"""
This class property holds the type argument(s) provided for the reified generic class.
Unlike `targ`, `type_args` always returns a tuple of the specified type arguments, even when there's only one type argument.
If no type arguments are given, it contains `Any`.
Returns:
tuple[type | Any, ...]: A tuple containing the type argument(s) given when the class was instantiated.
If no type argument was given, the returned tuple contains `Any`.
"""

def __new__(cls, *args, **kwargs):
# Prohibit from instantiating directly
if cls is Reified:
raise RuntimeError("Cannot instantiate 'Reified' class directly.")
return super().__new__(cls, *args, **kwargs)

# Return type should be inferred
def __class_getitem__(cls, params: type | tuple[type | Any, ...] | Any):
# Prohibit from instantiating directly
if cls is Reified:
raise RuntimeError("Cannot instantiate 'Reified' class directly.")
# Returns a separated reified type
param_tuple = tuplize_class_getitem_params(params)
rt = get_reified_type(cls, param_tuple)
rt.targ = params
rt.type_args = param_tuple
return rt
26 changes: 0 additions & 26 deletions reification/core.py

This file was deleted.

0 comments on commit fe3bea2

Please sign in to comment.