PEP 3107 Function Annotations for review and comment

Kay Schluehr kay.schluehr at gmx.net
Mon Jan 1 12:48:15 EST 2007


Tony Lownds wrote:
> On Dec 31, 2006, at 4:26 AM, Kay Schluehr wrote:
>
> > I have two questions:
> >
> > 1) I don't understand the clause ('*' [tname] (',' tname ['=' test])*
> > in the grammar rule of typedargslist. Does it stem from another PEP?
> >
>
> Yes, PEP 3102 Keyword-only Arguments.
>
> > 2) Is the func_annotation information for def foo(*c: list)
> > stored as {"*c": list} preserving optional argument information or
> > {"c":list} ?
> >
>
> {"c": list}
>
> -Tony

Good. There is still one issue. I understand that you don't want to fix
the semantics of function annotations but to be usefull some
annotations are needed to express function types. Using those
consistently with the notation of the enhanced function statement I
suggest introducing an arrow expression and an __arrow__ special
function:

expr: arrow_expr ('->' arrow_expr)*
arrow_expr: xor_expr ('|' xor_expr)*
...

class Algebraic(type):
    '''
    Metaclass used to enable operations on classes or subclasses
    '''
    def __init__(cls, name, bases, dict):
        super(Algebraic, cls).__init__(name, bases, dict)

    def __arrow__(cls, codom):
        fntype = Function(*cls.dom)
        fntype.codom = codom
        return fntype

def Function(*domains):
     "Function type generator"
    class FunType:
        __metaclass__ = Algebraic
        dom = domains
        codom = ()
    return FunType

maptype = Function( Function(object)->object, list) -> list




More information about the Python-list mailing list