File: generics.py

package info (click to toggle)
mkdocstrings-python-handlers 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,012 kB
  • sloc: python: 3,136; makefile: 42; sh: 17; javascript: 13
file content (49 lines) | stat: -rw-r--r-- 1,064 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Some module showing generics.

Type Aliases:
    SomeType: Some type alias.
"""

type SomeType[Z] = int | list[Z]
"""Some type alias.

Type parameters:
    Z: Some type parameter.
"""


class MagicBag[T: (str, bytes) = str](list[T]):
    """A magic bag of items.

    Type parameters:
        T: Some type.
    """

    def __init__[U: (int, bool)](self, *args: T, flag1: U | None = None, flag2: U | None = None) -> None:
        """Initialize bag.

        Type parameters:
            U: Some flag type.

        Parameters:
            flag1: Some flag.
            flag2: Some flag.
        """
        super().__init__(args)
        self.flag1 = flag1
        self.flag2 = flag2

    def mutate[K](self, item: T, into: K) -> K:
        """Shake the bag to mutate an item into something else (and eject it).

        Type parameters:
            K: Some other type.

        Parameters:
            item: The item to mutate.
            into: Mutate the item into something like this.

        Returns:
            The mutated item.
        """
        ...