Skip to content

recursive value expansion in PathExAttMap #74

@stolarczyk

Description

@stolarczyk

Is it intentional that the value expansion is performed only on the top level in attmap.PathExAttMap.__getitem__ method?

This makes the result of the literal value retrieval (lines 3 and 5) different from the same value retrieval (lines 4 and 6) in all the methods that rely on __getitem__, like items, to_dict etc.

The illustration uses a pipestat configuration file with environment variables read by yacman.YacAttMap, which inherits from attmap.PathExAttMap:

In [1]: from yacman import YacAttMap                                                                                                                                                                      

In [2]: y=YacAttMap(filepath="/Users/mstolarczyk/Desktop/testing/pypiper/pipestat_config.yaml")                                                                                                           

In [3]: y.database.port                                                                                                                                                                                   
Out[3]: '5432'

In [4]: y.to_dict(expand=True)["database"]["port"]                                                                                                                                                        
Out[4]: '$PRT'

In [5]: y.port                                                                                                                                                                                            
Out[5]: '5432'

In [6]: y.to_dict(expand=True)["port"]                                                                                                                                                                    
Out[6]: '5432'

In [7]: cat /Users/mstolarczyk/Desktop/testing/pypiper/pipestat_config.yaml                                                                                                                               
name: test
record_identifier: sample1
schema_path: sample_output_schema.yaml
port: $PRT
database:
  name: pipestat-test
  user: postgres
  password: pipestat-password
  host: localhost
  port: $PRT

A possible solution to this would be to change the current _safely_expand function from

def _safely_expand(x):
    return expandpath(x) if isinstance(x, str) else x

to

def _safely_expand(x):
     if isinstance(x, Mapping):
         return {k: _safely_expand(v) for k, v in x.items()}
    return expandpath(x) if isinstance(x, str) else x

I'm wondering if this would cause some serious downstream issues as most of our Python packages import this one.

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions