Skip to content

Use of string sentinel OMITTED = "<OMITTED>" with identity checks is fragile #1004

@mhliu0001

Description

@mhliu0001

While reading the option-handling code, I noticed that strax uses a string literal as a sentinel value:

OMITTED = "<OMITTED>"

and frequently checks it via identity, e.g.

if self.default is not OMITTED:
    ...

The usage of identity checks is fragile. We may want to use a more robust approach, like defining a dedicated class, implementing its deterministic hash, and defining how to unpickle the object. For example:

class _OmittedType:
    __slots__ = ()
    def __repr__(self): return "<OMITTED>"
    def __reduce__(self): return "strax.config._get_omitted", ()

def _get_omitted():
    return OMITTED

OMITTED = _OmittedType()

And in strax.utils.hashablize:

if obj is OMITTED:
    return "<OMITTED>"

Please let me know whether this makes sense. Happy to submit a PR for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions