python 3 import TypeError error
Question: I'm having a bizarre app startup error and was wondering if someone might know
how to debug/resolve it.

I'm running a Flask app using python 3. I kick things off by running: `python
manage.py runserver` and the stacktrace below is generated:

    
    
    Traceback (most recent call last):
      File "/projectbase/manage.py", line 19, in <module>
        create_tables()
      File "/projectbase/myproject/__init__.py", line 15, in create_tables
        from myproject.models.util import Weekday, weekday_type
      File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
      File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
      File "<frozen importlib._bootstrap>", line 742, in __exit__
      File "<frozen importlib._bootstrap>", line 742, in <genexpr>
    TypeError: an integer is required (got type NoneType)
    

**_The problem is, when I run it in a debugger and setup breakpoints the
problem goes away. But if I run it without debugging the problem exists._**

For further reference the `create_tables()` function called by manage.py
code/file looks like:

    
    
    from myproject.app import app
    from myproject.app import db
    
    def create_tables():
        from myproject.models.util import Weekday, weekday_type
        from myproject.models.activity import Activity, activities_tags, Occurrence, Tag
    
        db.create_all()
    

And further `Weekday` and `weekday_type` code in question looks like:

    
    
    from myproject.app import db
    from myproject.models.enum import DeclEnum
    
    class Weekday(DeclEnum):
        Sunday    = 'Sunday', {'offset': 0}
        Monday    = 'Monday', {'offset': 1}
        Tuesday   = 'Tuesday', {'offset': 2}
        Wednesday = 'Wednesday', {'offset': 3}
        Thursday  = 'Thursday', {'offset': 4}
        Friday    = 'Friday', {'offset': 5}
        Saturday  = 'Saturday', {'offset': 6}
    
    weekday_type = Weekday.db_type(metadata=db.metadata)
    weekday_type.register_with_psycopg(db.engine)
    

The `DeclEnum` type I use is basically just copied from this
[gist](https://gist.github.com/mbirtwell/aaccdb7972e1a7ed094b).

The only adjustment I made to the code was to make `DeclEnum` a little more
flexible so it can take a dictionary of values rather than just a `value` and
`description`. Other than that I changed nothing.

Any ideas what's going on here? Since the act of debugging it _fixes_ things,
I don't know how to go about getting to the bottom of the issue.

Thanks in advance.


Answer: The error is misleading.

Looking at the [gist](https://gist.github.com/mbirtwell/aaccdb7972e1a7ed094b)
I linked to in the original question

The method `register_with_psycopg()` works only if the enum type has first
been created in the database. I was calling the method before the type had
been created. By changing the ordering of things during app startup the error
went away.

