Skip to content

How should modifiers for keyword operators be spelled? #5930

@zygoloid

Description

@zygoloid

Summary of issue:

#5913 introduces a keyword operator for an unsafe conversion, and suggests spelling it as unsafe as (two keywords in sequence). When #845 considered the same question, the suggestion was instead to spell this kind of keyword-with-modifier as a single keyword, such as unsafe_as or assume_as or try_as.

Which approach do we prefer?

Details:

The simplest options here seem to be either:

  • Always use compound keywords: unsafe_as, try_as. This is simple and uninventive, and seems like the most conventional option. However, it's inflexible: what if we need an unsafe_try_as?
  • Always use modifier keywords: unsafe as, try as. This offers more flexibility, and is consistent with our approach to modifier keywords on declaration introducers. But it's somewhat novel and potentially surprising. There is prior art in Python's not in operator.

A third option to consider would be to say that it's unsafe that's special, not operator modifiers, and that we want a single unsafe keyword that's used consistently in all unsafe constructs for easy auditability. For example, we could eventually have: as, unsafe as, try_as, unsafe try_as.

Any other information that you want to share?

Multi-keyword operators would presumably allow arbitrary amounts of whitespace, including line breaks and line comments between the operators, which may be surprising but could be mitigated by making the formatter always keep them together on a line. There's also a possibility that multi-keyword operators may be harder to read, due to the possibility of misparsing them as (x unsafe) as y, but I'm not aware of that being a problem for python's a not in b, which could be misinterpreted as the valid-but-meaningless as a in not b.

Metadata

Metadata

Assignees

No one assigned

    Labels

    leads questionA question for the leads team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions