Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3 tests fail #84

Open
yurivict opened this issue Jul 9, 2024 · 1 comment
Open

3 tests fail #84

yurivict opened this issue Jul 9, 2024 · 1 comment

Comments

@yurivict
Copy link

yurivict commented Jul 9, 2024

========================================================================================= FAILURES ==========================================================================================
___________________________________________________________________________________________ test ____________________________________________________________________________________________

    def test():
        # Create private table
        private = PeriodicTable("mine")
        mass.init(private)
        density.init(private)
        xsf.init(private)
        nsf.init(private)
        crystal_structure.init(private)
        covalent_radius.init(private)
    
        # Test that it matches public table
        assert elements.Cm.mass == private.Cm.mass
        assert elements.Cm.density == private.Cm.density
>       assert elements.Cm.crystal_structure == private.Cm.crystal_structure
E       AttributeError: 'Element' object has no attribute 'crystal_structure'

test/test_private.py:19: AttributeError
___________________________________________________________________________________________ test ____________________________________________________________________________________________

    def test():
        #assert elements.Po.covalent_radius == 1.53
>       assert elements.Po.covalent_radius == 1.40
E       assert None == 1.4
E        +  where None = Po.covalent_radius
E        +    where Po = <periodictable.core.PeriodicTable object at 0x3e95bda14610>.Po

test/test_covalent_radius.py:5: AssertionError
___________________________________________________________________________________________ test ____________________________________________________________________________________________

    def test():
>       xtal = elements.Hg.crystal_structure
E       AttributeError: 'Element' object has no attribute 'crystal_structure'

test/test_crystal_structure.py:5: AttributeError
=============================================================================== 3 failed, 16 passed in 2.51s ================================================================================

Version: 1.7.1
python-3.11
FreeBSD 14.1

@pkienzle
Copy link
Owner

@yurivict thanks for the report.

This was hard to reproduce. It doesn't show up if el.crystal_structure happens to be accessed for some element before crystal_structure.init(private_table) is called in the first test.

The problem is with the delayed loader here:

clearprops()

When the crystal_structure attribute is set for the first time in an element in the private table it clears the loader required to access the property in the default table.

The following will fix it:

diff --git a/periodictable/core.py b/periodictable/core.py
index 7324eea..2951c92 100644
--- a/periodictable/core.py
+++ b/periodictable/core.py
@@ -130,6 +130,11 @@ def delayed_load(all_props, loader, element=True, isotope=False, ion=False):
         def setfn(el, value):
             #print "set", el, propname, value
             clearprops()
+            # Since the property is now cleared the loader will not be
+            # triggered in the getter for a different element, so call it here.
+            # This will update the default table even when the property is
+            # being initialized in a private table.
+            loader()
             setattr(el, propname, value)
         return setfn

Note sure if I'll produce a new 1.7 release before the 2.0 release waiting on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants