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

add support for dataclasses #5010

Merged
merged 23 commits into from Jun 5, 2018
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+15 −9
Diff settings

Always

Just for now

update dataclasses.field

  • Loading branch information...
Bogdanp committed Jun 4, 2018
commit 8b20ceb838a5630acca413642da858496210c807
@@ -120,7 +120,7 @@ from dataclasses import dataclass, field
@dataclass
class Person:
name: str
age: int = field(init=None) # E: Argument "init" to "field" has incompatible type "None"; expected "bool"
age: int = field(init=None) # E: No overload variant of "field" matches argument type "None"

[builtins fixtures/list.pyi]

@@ -1,4 +1,4 @@
from typing import Any, Callable, Generic, Optional, TypeVar, overload
from typing import Any, Callable, Generic, Mapping, Optional, TypeVar, overload

_T = TypeVar('_T')
_C = TypeVar('_C', bound=type)
@@ -29,11 +29,17 @@ def dataclass(_cls: None = ...,
frozen: bool = ...) -> Callable[[_C], _C]: ...


@overload
def field(*, default: _T,
init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
metadata: Optional[Mapping[str, Any]] = ...) -> _T: ...

@overload
def field(*, default_factory: Callable[[], _T],
init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
metadata: Optional[Mapping[str, Any]] = ...) -> _T: ...

@overload
def field(*,

This comment has been minimized.

Copy link
@ilevkivskyi

ilevkivskyi May 14, 2018

Collaborator

It is good that you added this to lib-stub. Be sure to make updates here, if python/typeshed#1944 gets updated.

This comment has been minimized.

Copy link
@ilevkivskyi

ilevkivskyi Jun 2, 2018

Collaborator

It looks like current definition in typeshed is a bit different, could you please sync this one?

This comment has been minimized.

Copy link
@Bogdanp

Bogdanp Jun 4, 2018

Author Contributor

This definition diverges from the one in typeshed in that it returns _T rather than Field[_T]. Returning Field[_T] breaks things like:

@dataclass
class Foo:
  x: int = field(default=42)

Because int and Field[int] are incompatible.

default: Optional[_T] = ...,
default_factory: Optional[Callable[..., _T]] = ...,
init: bool = ...,
repr: bool = ...,
hash: Optional[bool] = ...,
compare: bool = ...,
metadata: Any = ...) -> _T: ...
init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
metadata: Optional[Mapping[str, Any]] = ...) -> Any: ...
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.