I have a made a change to a python codebase which is causing mypy to fail with error:
Cannot instantiate abstract class "Feature" with abstract attributes "aggregator", "column_expression", "default_value" and "description_subject"
and I don't know why. Its true that class Feature
has those abstract attributes however I am not attempting to instantiate it (not intentionally anyway).
The code that mypy is objecting to is:
[
f[0](as_at=self.as_at, feature_period=f[1])
for f in (
(cls, fp)
for cls in self.FEATURE_CLASSES
for fp in self.feature_periods
)
]
This code instantiates a collection of classes named in self.FEATURE_CLASSES
. Each one of those classes is derived from abstract class Feature
.
The only change I made in the commit that caused this mypy failure was to add a new class to self.FEATURE_CLASSES
:
https://github.com/jamiekt/jstark/pull/4/files
classes Count
& GrossSpend
derive from the same base class as RecencyDays
so I can't understand why mypy is throwing this error. Furthermore, if I remove one of the other classes so its like this:
FEATURE_CLASSES = [Count, RecencyDays]
then mypy no longer throws an error. Hence its not something specific to RecencyDays
that is the problem, it seems to be the fact that FEATURE_CLASSES
now contains 3 items.
One more bit of information - when I run the code it runs successfully and as expected, as is proven by the successful completion of the unit test suite. Only the linting job (which includes mypy) fails. The unit tests jobs and failed linting job can be seen at https://github.com/jamiekt/jstark/actions/runs/3650289374.
Can anyone explain to me why this is happening and what I can do to fix it?
To reproduce the problem:
- clone the repo
- switch to the branch with the issue
- create a virtualenv and activate it
- install dependencies
- install mypy
- install stubs
- run mypy:
git clone https://github.com/jamiekt/jstark.git
cd jstark
git checkout recencydays-in-pfg
python3 -m venv venv
source venv/bin/activate
python3 -m pip install pyspark
python3 -m pip install mypy
mypy --install-types
mypy jstark/purchasing_feature_generator.py
It should return error:
jstark/purchasing_feature_generator.py:44: error: Cannot instantiate abstract class "Feature" with abstract attributes "aggregator", "column_expression", "default_value" and "description_subject" [abstract]
Found 1 error in 1 file (checked 1 source file)