Skip to content

Commit b3d5b4b

Browse files
committed
Fix structlog issue
1 parent 14e6d67 commit b3d5b4b

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

src/nomad_simulation_parsers/parsers/fhiaims/out_parser.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime
2-
from typing import Any, Optional, Union
2+
from typing import Any
33

44
import numpy as np
55
import pint
@@ -19,7 +19,7 @@
1919

2020

2121
# TODO move this to general utils
22-
def str_to_unit(val_in: str) -> Optional[pint.Unit]:
22+
def str_to_unit(val_in: str) -> pint.Unit | None:
2323
"""
2424
Convert string to pint.Unit
2525
"""
@@ -33,7 +33,7 @@ def str_to_unit(val_in: str) -> Optional[pint.Unit]:
3333

3434

3535
# TODO merge with str_to_scf_convergence
36-
def str_to_energy_components(val_in: str) -> dict[str, Union[float, pint.Quantity]]:
36+
def str_to_energy_components(val_in: str) -> dict[str, float | pint.Quantity]:
3737
"""
3838
Parse key, value pairs of energy components from string
3939
"""
@@ -52,7 +52,7 @@ def str_to_energy_components(val_in: str) -> dict[str, Union[float, pint.Quantit
5252
return res
5353

5454

55-
def str_to_scf_convergence(val_in: str) -> dict[str, Union[float, pint.Quantity]]:
55+
def str_to_scf_convergence(val_in: str) -> dict[str, float | pint.Quantity]:
5656
"""
5757
Parse key, value pairs of scf convergence parameters.
5858
"""
@@ -225,7 +225,7 @@ def str_to_gw_eigs(val_in: str) -> dict[str, np.ndarray]:
225225
return res
226226

227227

228-
def str_to_gw_scf(val_in: str) -> dict[str, Union[float, pint.Quantity]]:
228+
def str_to_gw_scf(val_in: str) -> dict[str, float | pint.Quantity]:
229229
"""
230230
Parse GW results
231231
"""
@@ -240,7 +240,7 @@ def str_to_gw_scf(val_in: str) -> dict[str, Union[float, pint.Quantity]]:
240240
return data
241241

242242

243-
def str_to_md_calculation_info(val_in: str) -> dict[str, Union[float, pint.Quantity]]:
243+
def str_to_md_calculation_info(val_in: str) -> dict[str, float | pint.Quantity]:
244244
"""
245245
Parse molecular dynamics parameters
246246
"""
@@ -264,7 +264,7 @@ def str_to_md_calculation_info(val_in: str) -> dict[str, Union[float, pint.Quant
264264
return res
265265

266266

267-
def str_to_quantity(val_in: str) -> Union[float, pint.Quantity, None]:
267+
def str_to_quantity(val_in: str) -> float | pint.Quantity | None:
268268
val = val_in.split()
269269
n = 2
270270
if len(val) == 1:

src/nomad_simulation_parsers/parsers/fhiaims/parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from nomad_simulation_parsers.parsers.utils.general import (
4141
remove_mapping_annotations,
4242
search_files,
43+
with_logger,
4344
)
4445
from nomad_simulation_parsers.schema_packages import fhiaims
4546

@@ -48,9 +49,9 @@
4849
LOGGER = get_logger(__name__)
4950

5051

52+
# TODO temporary fix for structlog unable to propagate logger
53+
@with_logger(logger=LOGGER)
5154
class FHIAimsOutMappingParser(TextMappingParser):
52-
# TODO temporary fix for structlog unable to propagate logger
53-
logger = LOGGER
5455

5556
_gw_flag_map = {
5657
'gw': 'G0W0',

src/nomad_simulation_parsers/parsers/utils/general.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import functools
22
import os
33
import re
4+
from collections.abc import Callable
45
from glob import glob
5-
from typing import TYPE_CHECKING, Any, Callable, Union
6+
from typing import TYPE_CHECKING, Any
67

78
if TYPE_CHECKING:
89
from structlog.stdlib import (
@@ -59,7 +60,7 @@ def remove_mapping_annotations(property: Section, max_depth: int = 5) -> None:
5960
using the same section as parent.
6061
"""
6162

62-
def _remove(property: Union[Section, SubSection], depth: int = 0):
63+
def _remove(property: Section | SubSection, depth: int = 0):
6364
if depth > max_depth:
6465
return
6566

@@ -88,6 +89,20 @@ def _remove(property: Union[Section, SubSection], depth: int = 0):
8889
_remove(property)
8990

9091

92+
def with_logger(
93+
cls=None,
94+
logger: 'BoundLogger' = get_logger(__name__)
95+
):
96+
def add_logger(cls):
97+
def add(cls):
98+
setattr(cls, 'logger', property(lambda self: logger))
99+
return cls
100+
101+
return add(cls)
102+
103+
return add_logger(cls) if cls else add_logger
104+
105+
91106
def log(
92107
function: Callable = None,
93108
logger: 'BoundLogger' = get_logger(__name__),

0 commit comments

Comments
 (0)