[Python-Dev] Is static typing still optional?
Eric V. Smith
eric at trueblade.com
Mon Dec 18 15:33:00 EST 2017
On 12/18/2017 2:55 PM, Ivan Levkivskyi wrote:
> On 18 December 2017 at 20:38, Nick Coghlan <ncoghlan at gmail.com
> <mailto:ncoghlan at gmail.com>> wrote:
>
>
> On 19 Dec. 2017 7:00 am, "Chris Barker" <chris.barker at noaa.gov
> <mailto:chris.barker at noaa.gov>> wrote:
>
>
> Are there other options??
>
> plain old:
>
> @dataclass
> class C:
> a = 1
> b = 1.0
>
> would work, though then there would be no way to express fields
> without defaults:
>
>
> The PEP already supports using "a = field(); b = field()" (etc) to
> declare untyped fields without a default value.
>
>
> The PEP is not 100% clear not this, but it is currently not the case and
> this may be intentional (one obvious way to do it),
> I just tried and this does not work:
>
> @dataclass
> class C:
> x = field()
>
> generates `__init__` etc. with no arguments. I think however that it is
> better to generate an error than silently ignore it.
> (Or if this a bug in the implementation, it should be just fixed.)
Hmm, not sure why that doesn't generate an error. I think it's a bug
that should be fixed. Or, we could make the same change we're making in
make_dataclass(), where we'll use "typing.Any" (as a string) if the type
is omitted. See https://bugs.python.org/issue32278.
More information about the Python-Dev
mailing list