Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Add option to allow None on specific TypedStruct field #48

Open
idanarye opened this issue Sep 3, 2017 · 2 comments
Open

Add option to allow None on specific TypedStruct field #48

idanarye opened this issue Sep 3, 2017 · 2 comments

Comments

@idanarye
Copy link

idanarye commented Sep 3, 2017

TypedStruct fields are forcing a type, but sometimes we want to explicitly allow None values:

class Foo(ts.TypedStruct):
    bar = ts.Field(int, allow_none=True)

foo1 = Foo(bar=None)

Questions:

  • Is allow_none a good name? Traditionally these types of fields would be called "optional", but this may be interpreted as "you don't need to supply a value"(which is what default= does), not necessarily as "you can put None in there"
  • Should allow_none imply default=None?
  • Do we want to limit this just for None values? It may be useful to also allow tokens. Maybe something like:
    class Foo(ts.TypedStruct)
        from easypy.tokens import AUTO, MAX, MIN
    
        bar = ts.Field(str, allow={None, AUTO})
        baz = ts.Field(int, allow={MIN, MAX})
    
    foo1 = Foo(bar=None, baz=MIN)
    foo2 = Foo(bar=AUTO, baz=MAX)

@koreno?

@koreno
Copy link
Contributor

koreno commented Sep 3, 2017

I'm not sure about the motivation.
It seems that if you're reading from some foreign source, and looking to create a TypedStruct from foreign data, there should be code that converts such None/MIN/MAX/AUTO into the appropriate types.
Or, Field should allow multiple types: ts.Field({str, NoneType, AUTO}). I don't think allow is very elegant...

@tigrawap
Copy link
Contributor

tigrawap commented Sep 3, 2017

I'd say - it as a road to everything a dict hell, make bunch great again.

typed should be typed, no optional types, if no value supplied(i.e kwargs unpacking from external source) and default is set - use default for initialising

Might also allow DEFAULT token, which should be used in typed functions definitions, which later chained to struct creation

This will indicate that default should be used. Plain None == exception

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants