Skip to content

Commit

Permalink
Fix: don't crash on dataclass.FieldInstance objects without a default.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 318389448
  • Loading branch information
rchen152 committed Jun 26, 2020
1 parent 819c7c1 commit a35c05f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pytype/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ def value_to_pytd_type(self, node, v, seen, view):
elif isinstance(v, typing_overlay.TypeVar):
return pytd.NamedType("__builtin__.type")
elif isinstance(v, dataclass_overlay.FieldInstance):
if not v.default:
return pytd.AnythingType()
return pytd_utils.JoinTypes(
self.value_to_pytd_type(node, d, seen, view)
for d in v.default.data)
Expand Down
15 changes: 15 additions & 0 deletions pytype/tests/py3/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,19 @@ class Foo(object):
def __init__(self, x: bool, y: int, z: str) -> None: ...
""")

def test_redefine_field(self):
# Tests that pytype can infer types for this (simplified) snippet of code
# from flax.struct.py.
ty = self.Infer("""
import dataclasses
def field(**kwargs):
return dataclasses.field(**kwargs)
""")
self.assertTypesMatchPytd(ty, """
from typing import Any
dataclasses: module
def field(**kwargs) -> Any: ...
""")


test_base.main(globals(), __name__ == "__main__")

0 comments on commit a35c05f

Please sign in to comment.