hgvs icon indicating copy to clipboard operation
hgvs copied to clipboard

Global configuration item not always being used

Open wlymanambry opened this issue 1 year ago • 1 comments

Describe the bug I set a global_config setting like:

global_config.normalizer.shuffle_direction = 5

I then call assembly_mapper:

 variant_mapper: assemblymapper.AssemblyMapper = assemblymapper.AssemblyMapper(
        connection,
        assembly_name="GRCh37",
        alt_aln_method="splign",
        normalize=1,
        prevalidation_level="INTRINSIC",
    )

with a print statement, I can see that this config value is set correctly here in assembly mapper: image

But when the Normalizer class is initialized, it is pulling whatever value is in the _data/defaults.ini file (and I'm not sure why)

A print here shows this is '3' instead of the expected 5 value that was set: image

To Reproduce Described above

Expected behavior For this configuration value to be set globally

wlymanambry avatar Apr 12 '24 22:04 wlymanambry

This is caused because Python evaluates default argument values once when the method is defined, rather than at call time, so these are setting the value when the program started up NOT the value after you change it.

So when you import this code:

class Normalizer:
    """Perform variant normalization"""

    def __init__(
        self,
        hdp,
        cross_boundaries=hgvs.global_config.normalizer.cross_boundaries,
        shuffle_direction=hgvs.global_config.normalizer.shuffle_direction,
        alt_aln_method=hgvs.global_config.mapping.alt_aln_method,
        validate=hgvs.global_config.normalizer.validate,
        variantmapper=None,
    ):

It is being converted to eg:

In [7]: from hgvs.normalizer import Normalizer

In [8]: Normalizer.__init__?
Signature:
Normalizer.__init__(
    self,
    hdp,
    cross_boundaries=False,
    shuffle_direction=3,
    alt_aln_method='splign',
    validate=True,
    variantmapper=None,
)

So there are 2 fixes:

a) Make global_config read only (throwing an exception in setattr) saying that it is designed to run off ini files and is NOT modifiable

b) Go through and replace all default arguments set from global config with None, then in the constructor, check if is not None then set off global config (this will pull in the present value)

davmlaw avatar Dec 23 '24 05:12 davmlaw