Skip to content

[Bug] _RestrictedProxy does not invoke __format__ correctly when called on class #850

@brandonchinn178

Description

@brandonchinn178

What are you really trying to do?

Note: this issue came about because we implemented our own from_payload from scratch. I just rewrote the code to reuse JSONPlainPayloadConverter and just define our own custom_type_converters instead of reimplementing the whole thing. So this doesn't affect us anymore, but it's still a confusing error to get

We're defining our own custom from_payload function that deserializes according to the type_hint. If the type_hint isn't supported, we throw an error like

raise ValueError(f"{val} does not match {type_hint}")

When using a dataclass with a datetime field, this errors with

TypeError: descriptor '__format__' for 'datetime.date' objects doesn't apply to a 'str' object

Related: #301

Describe the bug

Normally, running

from datetime import datetime
format(datetime, "")

will desugar as type(datetime).__format__(datetime, ""), where type(datetime) is type and type.__format__ just calls str().

But when datetime is a _RestrictedProxy, format(proxy_datetime, "") will run _RestrictedProxy.__format__(proxy_datetime, ""), which will run real_datetime.__format__(""), which throws the above error.

Current workaround is to explicitly convert to str like f"{type_hint!s}", but the error is very confusing

Minimal Reproduction

Environment/Versions

  • OS and processor: [e.g. M1 Mac, x86 Windows, Linux]
  • Temporal Version: [e.g. 1.14.0?] and/or SDK version
  • Are you using Docker or Kubernetes or building Temporal from source?

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions