podhmo / egoist

egoistic framework framework
0 stars 0 forks source link

NewNamedType() #55

Closed podhmo closed 4 years ago

podhmo commented 4 years ago

refs #38

use https://github.com/podhmo/metashape/pull/52

From now on, define named type by NewNamedType()

import typing as t
from egoist.types import NewNamedType

class X:
    pass

class Y:
    pass

XorY = NewNamedType("XorY", t.Union[X,Y])

future work

changes to type-checkeble in definitions.py, currently not supported yet.

import typing as tx
from egoist.typing import resolve_name, NewNamedType

Direction = NewNamedType("Direction", tx.Literal["N", "S", "W", "E"])

def use(d: Direction) -> None:
    print("@", d)

print(resolve_name(Direction))
use("N")
use("X")
$ mypy --strict --strict-equality --ignore-missing-imports 00resolve.py
00resolve.py:7: error: Variable "00resolve.Direction" is not valid as a type
00resolve.py:7: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases