2021-12-27 08:29:09 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2022-03-21 13:03:22 +00:00
|
|
|
import types
|
2021-11-24 11:06:11 +00:00
|
|
|
|
2024-08-29 15:35:04 +00:00
|
|
|
from typing import Protocol
|
|
|
|
|
2022-03-21 13:03:22 +00:00
|
|
|
import pytest
|
2021-11-24 11:06:11 +00:00
|
|
|
|
2023-08-06 19:33:03 +00:00
|
|
|
import attr
|
|
|
|
|
2021-11-24 11:06:11 +00:00
|
|
|
|
|
|
|
@pytest.fixture(name="mp")
|
|
|
|
def _mp():
|
2022-03-21 13:03:22 +00:00
|
|
|
return types.MappingProxyType({"x": 42, "y": "foo"})
|
2021-11-24 11:06:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestMetadataProxy:
|
|
|
|
"""
|
2022-03-21 13:03:22 +00:00
|
|
|
Ensure properties of metadata proxy independently of hypothesis strategies.
|
2021-11-24 11:06:11 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_repr(self, mp):
|
|
|
|
"""
|
|
|
|
repr makes sense and is consistent across Python versions.
|
|
|
|
"""
|
|
|
|
assert any(
|
|
|
|
[
|
|
|
|
"mappingproxy({'x': 42, 'y': 'foo'})" == repr(mp),
|
|
|
|
"mappingproxy({'y': 'foo', 'x': 42})" == repr(mp),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_immutable(self, mp):
|
|
|
|
"""
|
|
|
|
All mutating methods raise errors.
|
|
|
|
"""
|
|
|
|
with pytest.raises(TypeError, match="not support item assignment"):
|
|
|
|
mp["z"] = 23
|
|
|
|
|
|
|
|
with pytest.raises(TypeError, match="not support item deletion"):
|
|
|
|
del mp["x"]
|
|
|
|
|
|
|
|
with pytest.raises(AttributeError, match="no attribute 'update'"):
|
|
|
|
mp.update({})
|
|
|
|
|
|
|
|
with pytest.raises(AttributeError, match="no attribute 'clear'"):
|
|
|
|
mp.clear()
|
|
|
|
|
|
|
|
with pytest.raises(AttributeError, match="no attribute 'pop'"):
|
|
|
|
mp.pop("x")
|
|
|
|
|
|
|
|
with pytest.raises(AttributeError, match="no attribute 'popitem'"):
|
2021-11-24 11:10:13 +00:00
|
|
|
mp.popitem()
|
2021-11-24 11:06:11 +00:00
|
|
|
|
|
|
|
with pytest.raises(AttributeError, match="no attribute 'setdefault'"):
|
|
|
|
mp.setdefault("x")
|
2023-08-06 19:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_attrsinstance_subclass_protocol():
|
|
|
|
"""
|
|
|
|
It's possible to subclass AttrsInstance and Protocol at once.
|
|
|
|
"""
|
|
|
|
|
2024-08-29 15:35:04 +00:00
|
|
|
class Foo(attr.AttrsInstance, Protocol):
|
2024-01-26 07:15:53 +00:00
|
|
|
def attribute(self) -> int: ...
|