Skip to content
Snippets Groups Projects
goose.py 1.57 KiB
Newer Older
  • Learn to ignore specific revisions
  • q3k's avatar
    q3k committed
    """
    
    q3k's avatar
    q3k committed
    Goose implements Goose Typing (tm), a set of shims which allows the st3m
    
    q3k's avatar
    q3k committed
    codebase to use both Python type annotations, Abstract Base Classes and run
    under Micropython.
    """
    
    # Detect whether we're in MyPy or just executing.
    try:
        from typing import TYPE_CHECKING
    except:
        TYPE_CHECKING = False
    
    if TYPE_CHECKING:
        # We're in MyPy.
        from abc import ABCMeta, abstractmethod
    
    q3k's avatar
    q3k committed
    
    
    q3k's avatar
    q3k committed
        class ABCBase(metaclass=ABCMeta):
            pass
    
    
        from typing import (
            List,
            Optional,
            Tuple,
            Dict,
            Any,
            Callable,
            Iterator,
            Generator,
            Union,
        )
    
    q3k's avatar
    q3k committed
        from enum import Enum
    else:
        # We're in CPython or Micropython.
        class ABCBase:
            pass
    
        def abstractmethod(f):
            def _fail():
                raise Exception("abstract method call")
    
    q3k's avatar
    q3k committed
    
    
    q3k's avatar
    q3k committed
            return _fail
    
        try:
    
            from typing import (
                List,
                Optional,
                Tuple,
                Dict,
                Any,
                Callable,
                Iterator,
                Generator,
    
    q3k's avatar
    q3k committed
            from enum import Enum
        except ImportError:
            # We're in Micropython.
            List = None
            Optional = None
    
            Tuple = None
    
    q3k's avatar
    q3k committed
            Dict = None
            Any = None
    
            Callable = None
    
            Generator = None
    
            Union = None
    
    q3k's avatar
    q3k committed
    
            class Enum:
                pass
    
    
    
    q3k's avatar
    q3k committed
    __all__ = [
        "TYPE_CHECKING",
        "ABCBase",
        "abstractmethod",
        "List",
        "Optional",
        "Enum",
    
        "Tuple",
    
    q3k's avatar
    q3k committed
        "Dict",
        "Any",
    
        "Generator",
    
    q3k's avatar
    q3k committed
    ]