Пример #1
0
def test_rgetattr(separator):
    """Test to ensure recursive model access works.
    GUI elements are named f"prefix{_}" "recursive{_}attr" and bind to recursive.attr.

    https://stackoverflow{_}com/a/31174427/
    """
    p = Person()
    _ = separator

    # Test rgetattr(present)
    assert rgetattr(p, f"pet{_}species") == "Dog"
    assert rgetattr(p, f"pet{_}species", object()) == "Dog"

    # Test rgetattr(missing)
    assert rgetattr(p, f"pet{_}ghost{_}species", "calico") == "calico"
    with pytest.raises(AttributeError):
        # Without a default argument, `rgetattr`, like `getattr`, raises
        # AttributeError when the dotted attribute is missing
        print(rgetattr(p, f"pet{_}ghost{_}species"))

    # Test rsetattr()
    rsetattr(p, f"pet{_}name", "Sparky")
    rsetattr(p, f"residence{_}type", "Apartment")
    assert p.pet.name == "Sparky"
    assert p.residence.type == "Apartment"

    # Test rhasattr()
    assert rhasattr(p, f"pet")
    assert rhasattr(p, f"pet{_}name")

    # Test rhasattr(levels of missing)
    assert not rhasattr(p, f"pet{_}ghost")
    assert not rhasattr(p, f"pet{_}ghost{_}species")
    assert not rhasattr(p, f"ghost")
    assert not rhasattr(p, f"ghost{_}species")
Пример #2
0
def test_rgetattr_broken(separator):
    """
    rgetattr(default) fails to short-circuit/return on the first missing attribute.
    I never use rgetattr(default) so I won't bother fixing the bug.

    Wrong answer:
    - None.foo AKA 1
    - 1.bar AKA 1
    - 1.imag == 0

    Right answer:
    - None.foo AKA return 1 to caller
    """
    _ = separator

    result = rgetattr(object(), f"nothing{_}imag", 1)
    assert result == 1, result
Пример #3
0
 def getter(self: "ConfigModel") -> str:
     return rgetattr(self.cfg, path)
Пример #4
0
 def getter(self: "ConfigModel"):
     val = rgetattr(self.cfg, path)
     if val is None:
         return default
     else:
         return val