I was working on a project using importlib
in which I needed to locate the relevant enum class within a file.
import importlib from enum import EnumType imp = importlib.import_module(f'{package_name}.c.{self.name}') # get the Python file for name, value in self.imp.__dict__.items(): if isinstance(value, EnumType): # check if element is Enum return value raise ValueError(f'Unable to identify category enum for concept "{self.name}".')
In order to check if the element is an enum.Enum
, I looked up the documentation and found the appropriate isinstance
check:

This worked brilliantly.
A collaborator ended up using the code but reported an error: ImportError: cannot import 'EnumType' from 'enum'
. I somehow thought I was above such errors — unable to import a standard library module?
After some troubleshooting and exploration (even resorting to asking a LLM…), I discovered the answer in CPython’s Github issues which pointed me to Python 3.11’s changelog:

I like EnumType
a lot better than EnumMeta
(what is a ‘meta’?), but it was difficult to track down the change. Usually, the Python doco is incredibly good at listing ‘New in version 3.x’, but it was absent. I had checked the 3.11 docs after learning that my collaborator was running 3.10, but didn’t see anything on the page:

In fact, the text ‘EnumMeta’ doesn’t appear anywhere on the page. An easy oversight… Anyway, here’s the updated code which my collaborator is able to run:
import importlib from enum import EnumMeta # an alias of EnumType in 3.11+, so can still use it imp = importlib.import_module(f'{package_name}.c.{self.name}') # get the Python file for name, value in self.imp.__dict__.items(): if isinstance(value, EnumMeta): # check if element is Enum return value raise ValueError(f'Unable to identify category enum for concept "{self.name}".')