Anurag-S1ngh commented on code in PR #41334:
URL: https://github.com/apache/beam/pull/41334#discussion_r4578059798


##########
sdks/python/apache_beam/typehints/opcodes.py:
##########
@@ -485,14 +506,39 @@ def _getattr(o, name):
     return Const(BoundMethod(func, o))
   elif isinstance(o, row_type.RowTypeConstraint):
     return o.get_type_for(name)

Review Comment:
   Thanks, fixed in 3319537. The field type is normalized now, so nullable 
nested rows flatten into a single union. Added 
testRowTypeAttrNormalizesFieldType as a regression test.



##########
sdks/python/apache_beam/typehints/opcodes.py:
##########
@@ -485,14 +506,39 @@ def _getattr(o, name):
     return Const(BoundMethod(func, o))
   elif isinstance(o, row_type.RowTypeConstraint):
     return o.get_type_for(name)
-  elif inspect.isclass(o) and dataclasses.is_dataclass(o):
-    field = o.__dataclass_fields__.get(name)
+  elif inspect.isclass(o):
+    if match_is_named_tuple(o):
+      field_type = o.__annotations__.get(name)
+      if field_type is None:
+        return Any
+      try:
+        return typehints.normalize(field_type)
+      except Exception:  # Inference should never fail.
+        return Any
+    field = (o.__dataclass_fields__.get(name)
+             if dataclasses.is_dataclass(o) else None)
     if field is not None:
       return resolve_dataclass_field_type(field.type)
     return Any
-
   else:
-    return Any
+    try:
+      normalized = typehints.normalize(o)
+    except Exception:
+      return Any
+    if not isinstance(normalized, typehints.UnionHint.UnionConstraint):
+      return Any
+    has_none = type(None) in normalized.union_types
+    resolved = [
+        _getattr(member, name)
+        for member in normalized.union_types
+        if member is not type(None)
+    ]
+    if not resolved or any(result is Any for result in resolved):

Review Comment:
   Thanks, fixed in 3319537. _getattr returns Any when a resolved member is a 
Const, and testOptionalMethodAccess covers method access on an Optional row.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to