armi icon indicating copy to clipboard operation
armi copied to clipboard

Can we replace the LocationBase class?

Open john-science opened this issue 11 months ago • 3 comments

We have a class called LocationBase:

https://github.com/terrapower/armi/blob/6febc6529f4f8430fe56aa67d4f68ae805469324/armi/reactor/grids/locations.py#L30

But in the class docstring it says that this was only needed because of a bug in Python 2:

https://github.com/terrapower/armi/blob/6febc6529f4f8430fe56aa67d4f68ae805469324/armi/reactor/grids/locations.py#L38-L41

Since ARMI now only supports Python 3.7+, it would seem we can replace this class with a much simper namedtuple. That would simplify our codebase.

Less is more.

john-science avatar Feb 07 '25 19:02 john-science

Can we use abc.ABC with namedtuples? It's inherited in a couple places and has some abstract methods

Maybe possible. I don't have experience subclassing namedtuples

drewj-tp avatar Feb 07 '25 20:02 drewj-tp

Can we use abc.ABC with namedtuples? It's inherited in a couple places and has some abstract methods

There is only one method that the @abstractmethod decoratorin that class,indicies(), it would be an easy replacement:

from collections import namedtuple

class LocationBase(numedtuple):
    @property
    def indices(self):
        raise NotImplementedError()

I'm sure there other ways, of course. Just the first one to come to mind.

john-science avatar Feb 07 '25 21:02 john-science

Maybe dataclass? I tend to prefer those if I want to make a simple class that is more akin to a c style struct. There's some discussion in the PEP comparing dataclass to namedtuple

And, this isn't really the point, but making an abstract method raise NotImplementedError is not a replacement. The former means someone cannot instantiate a subclass unless the abstract methods are implemented. The latter means you maybe won't see your subclass is fully compliant until a runtime problem calls the parent's method.

drewj-tp avatar Feb 10 '25 20:02 drewj-tp