[configTools] Make Option eq=False so cmp/hash by id
This commit is contained in:
parent
ade9ce1173
commit
e9bec87d1a
@ -86,7 +86,8 @@ class ConfigUnknownOptionError(ConfigError):
|
||||
super().__init__(f"Config option {name} is unknown")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
# eq=False because Options are unique, not fungible objects
|
||||
@dataclass(frozen=True, eq=False)
|
||||
class Option:
|
||||
name: str
|
||||
"""Unique name identifying the option (e.g. package.module:MY_OPTION)."""
|
||||
|
@ -1,6 +1,12 @@
|
||||
import dataclasses
|
||||
import pytest
|
||||
|
||||
from fontTools.misc.configTools import AbstractConfig, Options, ConfigUnknownOptionError
|
||||
from fontTools.misc.configTools import (
|
||||
AbstractConfig,
|
||||
Option,
|
||||
Options,
|
||||
ConfigUnknownOptionError,
|
||||
)
|
||||
|
||||
|
||||
def test_can_create_custom_config_system():
|
||||
@ -32,3 +38,22 @@ def test_can_create_custom_config_system():
|
||||
# Test that it raises on unknown option
|
||||
with pytest.raises(ConfigUnknownOptionError):
|
||||
cfg.get("test:unknown")
|
||||
|
||||
|
||||
def test_options_are_unique():
|
||||
class MyConfig(AbstractConfig):
|
||||
options = Options()
|
||||
|
||||
opt1 = MyConfig.register_option("test:OPT_1", "", "foo", str, any)
|
||||
cfg = MyConfig({opt1: "bar"})
|
||||
assert cfg[opt1] == "bar"
|
||||
|
||||
opt2 = Option("test:OPT_1", "", "foo", str, any)
|
||||
|
||||
assert dataclasses.asdict(opt1) == dataclasses.asdict(opt2)
|
||||
assert opt1 != opt2
|
||||
|
||||
with pytest.raises(ConfigUnknownOptionError):
|
||||
cfg.get(opt2)
|
||||
with pytest.raises(ConfigUnknownOptionError):
|
||||
cfg.set(opt2, "bar")
|
||||
|
Loading…
x
Reference in New Issue
Block a user