Skip to content
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
Closed

Always require optional field in python target #70

kanghyojun opened this issue Aug 24, 2016 · 4 comments
Labels
cmp:compiler Component: Compiler backend (e.g., annotation processors, code generators) target:python typ:bug Type: Bug/defect

Comments

@kanghyojun
Copy link
Member

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 Type: Bug/defect label Aug 24, 2016
@dahlia
Copy link
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
Copy link
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
Copy link
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
Copy link
Contributor

👀

@dahlia dahlia added cmp:compiler Component: Compiler backend (e.g., annotation processors, code generators) target:python labels Aug 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmp:compiler Component: Compiler backend (e.g., annotation processors, code generators) target:python typ:bug Type: Bug/defect
Projects
None yet
Development

No branches or pull requests

3 participants