New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always require optional field in python target #70

Closed
kanghyojun opened this Issue Aug 24, 2016 · 4 comments

Comments

Projects
None yet
3 participants
@kanghyojun
Member

kanghyojun commented Aug 24, 2016

record foo (
  text? optional-data
);

Above foo record compiled to

class Foo:
    ...

    def __init__(self, optional_data: typing.Optional[str]) -> None:
        ...

It requires optional_data as a argument to initiate class Foo, although optional_data is optional.

@kanghyojun kanghyojun added the typ:bug label Aug 24, 2016

@dahlia

This comment has been minimized.

Member

dahlia commented Aug 29, 2016

Should we treat it as a bug? I'm not sure. Think of the following example:

record bar (
    text a,
    text? b,
    text c,
    text? d,
);

It consists of two required fields and two optional fields, and they appear by turns. So should it be compiled to __init__(self, a: str, b: Optional[str]=None, c: str, d: Optional[str]=None)? As you probably are aware of, it is not a valid Python syntax since Python doesn't allow optional argument followed by required argument.

@dahlia

This comment has been minimized.

Member

dahlia commented Aug 29, 2016

There could be some ways. One way is making __init__() method to take only keyword arguments (e.g. __init__(self, *, a: str, b: Optional[str]=None, c: str, d: Optional[str]=None)).

@dahlia

This comment has been minimized.

Member

dahlia commented Dec 22, 2016

Now it becomes harder to be keyword-only than before, since now Python target has Python 2 generator as well. Whereas Python 3 has a operator for keyword-only parameters (PEP 3102), Python 2 does not. In order to implement keyword-only parameters in Python 2, we need some tricks:

def __init__(self, **kwargs):
    invalid_params = frozenset(kwargs) - {'a', 'b', 'c', 'd'}
    if invalid_params:
        raise TypeError('unexpected keywords: ' + ', '.join(invalid_params))
    ...

@dahlia dahlia assigned dahlia and unassigned dahlia Aug 14, 2017

@earlbread

This comment has been minimized.

Contributor

earlbread commented Aug 14, 2017

👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment