I'd like to use an instance of an inner class (in this case a namedtuple although the exact same symptoms occur for an inner class defined with class) as a default value for an outer class method (in this case the constructor). However, when this code is imported from a different module the outer class definition seems to be missing.
Example:
# mymodule.py
from typing import NamedTuple, Tuple
class IdSignal():
Cfg = NamedTuple('IdSignalCfg', [
('nfft', int),
('limits', Tuple[float, float]),
('min_spacing', float),
('nmix', int)])
Cfg.__new__.__defaults__ = (
512,
(1500, 7500),
200,
3
)
def __init__(self, cfg = IdSignal.Cfg()):
self.cfg = cfg
Now executing import mymodule throws:
Exception has occurred: NameError
name 'IdSignal' is not defined
File "...", line 18, in IdSignal
def __init__(self, cfg = IdSignal.Cfg()):
File "...", line 5, in <module>
class IdSignal():
...
import mymodule
Confusingly, both pylint and mypy don't recognize any error in the above code.
Can this be achieved any other way?
I understand I can use None as a default value and instantiate IdSignal.Cfg within the constructor. If this is the only solution, I'd like to understand why is the above code failing?
IdSignalis the outer class; the inner class is the namedtuple typeCfg.