问题
I am working on a Flask Application with a Postgres database.
When I run nose tests locally everything works fine, but when I upload the code to GitLab this happens in my pipeline. I am using gitlab-ci
. Any suggestions on how to solve this issue are welcome.
$ nosetests --with-coverage --cover-package=app
EEEEE
======================================================================
ERROR: Failure: TypeError (can't apply this __setattr__ to DefaultMeta object)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/builds/Mubangizi1/mobile_shop_backend/venv/lib/python3.8/site-packages/nose/failure.py", line 39, in runTest
raise self.exc_val.with_traceback(self.tb)
File "/builds/Mubangizi1/mobile_shop_backend/venv/lib/python3.8/site-packages/nose/loader.py", line 417, in loadTestsFromName
module = self.importer.importFromPath(
File "/builds/Mubangizi1/mobile_shop_backend/venv/lib/python3.8/site-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/builds/Mubangizi1/mobile_shop_backend/venv/lib/python3.8/site-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/usr/local/lib/python3.8/imp.py", line 244, in load_module
return load_package(name, filename)
File "/usr/local/lib/python3.8/imp.py", line 216, in load_package
return _load(spec)
File "<frozen importlib._bootstrap>", line 702, in _load
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/builds/Mubangizi1/mobile_shop_backend/app/controllers/__init__.py", line 2, in <module>
from .product import (ProductDetailView, ProductView)
File "/builds/Mubangizi1/mobile_shop_backend/app/controllers/product.py", line 6, in <module>
from app.models.product import Product
File "/builds/Mubangizi1/mobile_shop_backend/app/models/__init__.py", line 3, in <module>
db = SQLAlchemy()
File "/builds/Mubangizi1/mobile_shop_backend/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 715, in __init__
self.Model = self.make_declarative_base(model_class, metadata)
File "/builds/Mubangizi1/mobile_shop_backend/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 797, in make_declarative_base
model.query_class = self.Query
File "/builds/Mubangizi1/mobile_shop_backend/venv/lib/python3.8/site-packages/sqlalchemy/ext/declarative/api.py", line 79, in __setattr__
_add_attribute(cls, key, value)
File "/builds/Mubangizi1/mobile_shop_backend/venv/lib/python3.8/site-packages/sqlalchemy/ext/declarative/base.py", line 802, in _add_attribute
type.__setattr__(cls, key, value)
TypeError: can't apply this __setattr__ to DefaultMeta object
======================================================================
ERROR: Failure: TypeError (can't apply this __setattr__ to DefaultMeta object)
----------------------------------------------------------------------
.
.
.
Ran 5 tests in 0.550s
FAILED (errors=5)
ERROR: Job failed: exit code 1
This is my .gitlab-ci.yml
file containing the pipeline configurations.
image: python:latest
# # Change pip's cache directory to be inside the project directory since we can
# # only cache local items.
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
cache:
paths:
- .cache/pip
- venv/
before_script:
- python -V # Print out python version for debugging
- pip install virtualenv
- virtualenv venv
- source venv/bin/activate
stages:
- test
test:
stage: test
services:
- postgres:alpine
variables:
POSTGRES_DB: mobile_shop_test_db
POSTGRES_USER: postgres
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_PASSWORD: password
script:
- export FLASK_APP=server.py
- export FLASK_ENV=testing
- export DATABASE_TEST_URI=postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/mobile_shop_test_db
- export FLASK_APP_SECRET=qY2i691SX2sEuZ7LUjY180RS98mw3qCeUiyV0i0vzmg
- apt-get update -qy
- apt-get install -y python-dev python-pip
- pip install -r requirements.txt
- nosetests --with-coverage --cover-package=app
- codecov
回答1:
This looks like an issue that was introduced into the python language.
To fix this:
You can revert the version of python you are building with, or if you are using docker, fix the python image temporarily using python:3.8.3-slim or an equivalent image. You can also wait until the fix is out.
You can see the related pull request here: https://github.com/python/cpython/pull/21473
Introduced here: https://bugs.python.org/issue39960
Patch bug here: https://bugs.python.org/issue41295
Another issue in an unrelated package that is the same error: flask_sqlalchemy: error with `__setattr__` to `DefaultMeta`
Edit: A newer version with the fix is out. Check out any image of python:3.8.5 +
来源:https://stackoverflow.com/questions/62897377/git-lab-ci-running-nose-tests-with-sqlalchemy-error-failure-typeerror-cant