Repository: plotly/dash
File: dash/dependencies.py (Python)

Code before:
    allowed_wildcards: Sequence[_Wildcard]
    allow_optional: bool

    def __init__(self, component_id: ComponentIdType, component_property: str):

        if isinstance(component_id, Component):
            self.component_id = component_id._set_random_id()
        else:
            self.component_id = component_id

        self.component_property = component_property
        self.allow_duplicate = False
        self.allow_optional = False

    def __str__(self):
        return f"{self.component_id_str()}.{self.component_property}"

    def __repr__(self):
        return f"<{self.__class__.__name__} `{self}`>"

    def component_id_str(self) -> str:
        return stringify_id(self.component_id)

    def to_dict(self) -> dict:
        specs = {"id": self.component_id_str(), "property": self.component_property}
        return {**specs, "allow_optional": True} if self.allow_optional else specs

    def __eq__(self, other):
        """
        We use "==" to denote two deps that refer to the same prop on
        the same component. In the case of wildcard deps, this means
        the same prop on *at least one* of the same components.
        """
        return (
            isinstance(other, DashDependency)
            and self.component_property == other.component_property
            and self._id_matches(other)
        )

    def _id_matches(self, other) -> bool:
        my_id = self.component_id
        other_id = other.component_id
        self_dict = isinstance(my_id, dict)
        other_dict = isinstance(other_id, dict)

        if self_dict != other_dict:
            return False
        if self_dict:
            if set(my_id.keys()) != set(other_id.keys()):
                return False


Review comment (suggestion):
This is kinda weird to read, should be:
```python
if self.allow_optional:
    specs["allow_optional"] = True
return specs
```


Code after:
    allowed_wildcards: Sequence[_Wildcard]
    allow_optional: bool

    def __init__(self, component_id: ComponentIdType, component_property: str):

        if isinstance(component_id, Component):
            self.component_id = component_id._set_random_id()
        else:
            self.component_id = component_id

        self.component_property = component_property
        self.allow_duplicate = False
        self.allow_optional = False

    def __str__(self):
        return f"{self.component_id_str()}.{self.component_property}"

    def __repr__(self):
        return f"<{self.__class__.__name__} `{self}`>"

    def component_id_str(self) -> str:
        return stringify_id(self.component_id)

    def to_dict(self) -> dict:
        specs = {"id": self.component_id_str(), "property": self.component_property}
        if self.allow_optional:
            specs["allow_optional"] = True
        return specs

    def __eq__(self, other):
        """
        We use "==" to denote two deps that refer to the same prop on
        the same component. In the case of wildcard deps, this means
        the same prop on *at least one* of the same components.
        """
        return (
            isinstance(other, DashDependency)
            and self.component_property == other.component_property
            and self._id_matches(other)
        )

    def _id_matches(self, other) -> bool:
        my_id = self.component_id
        other_id = other.component_id
        self_dict = isinstance(my_id, dict)
        other_dict = isinstance(other_id, dict)

        if self_dict != other_dict:
            return False
        if self_dict:
            if set(my_id.keys()) != set(other_id.keys()):