Improve clarity in Sage tests files swapping loads and imports
Dioprz opened this issue · 0 comments
Working at test_sdfq.sage my LSP warning me about the next code being inconsistent because we give two inputs to log, but only one is required (as we are really using log2
).
from math import comb as binomial, log2 as log
def lee_brickell_correction(k):
return log(k, 2) * 2 - log(binomial(k, 2), 2)
Which is weird because that is true, but our tests are working.
After some debugging I found the culprit to be the intermediate Sage load functions:
from math import comb as binomial, log2 as log
load("tests/references/helpers/attack_cost.sage")
load("tests/references/helpers/cost.sage")
...
def lee_brickell_correction(k):
return log(k, 2) * 2 - log(binomial(k, 2), 2)
Because loading is equivalent to execute the Sage file within the global scope, the normal log
import used in attack_cost.sage
was overriding our log2 as log
, allowing all the subsequent code work.
Proposed solution
Since this situation is very tricky, and can easily become a nightmare to debug, we should swap the load
and import
declarations in every file, making each file completely predictable in its scope.
Previous example would end up like this:
load("tests/references/helpers/attack_cost.sage")
load("tests/references/helpers/cost.sage")
from math import comb as binomial, log2 as log
...
def lee_brickell_correction(k):
return log(k, 2) * 2 - log(binomial(k, 2), 2)
Which will produce errors, but those are predictable and easy to fix.
I will start making these changes on the files I have access to while making the tests
directory refactor, and let the others for later.