Skip to content

Do not replace Optional with | None in ImplicitDict fields #1221

@BenjaminPelletier

Description

@BenjaminPelletier

Describe the bug
Currently, our autoformatters replace instances of Optional[ConcreteType] with ConcreteType | None. This results in a loss of clarity for expectations regarding ImplicitDict fields. For instance, this is more clear:

class Foo(ImplicitDict):
    baz: Optional[str]

...than

class Bar(ImplicitDict):
    baz: str | None

When a user is considering what to provide to populate the baz field, the Foo class type annotation reads as "this field may optionally contain a str" with the implication that it doesn't need to be provided since it's optional. The Bar class type annotation reads as "this field may be a str or it may be None" with the implication that a value with one of these types needs to be provided.

To reproduce

  • Write an ImplicitDict class declare one or more fields with Optional[ConcreteType] type
  • Run make format
  • Observe that the fields have been rewritten to have the type ConcreteType | None

Difference from expected behavior
I expect to be able to annotate ImplicitDict fields as being optional using the word "Optional".

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal prioritybugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions