Reputation: 221
I ran flake8 in a GitHub action for my project and got a failure.
It seems the offensive line in the source is:
response = test_app_with_db.get(f"/summaries/")
and the Github Action result is:
Run docker exec fastapi-tdd python -m flake8 .
docker exec fastapi-tdd python -m flake8 .
shell: /bin/bash -e {0}
env:
IMAGE: docker.pkg.github.com/$GITHUB_REPOSITORY/web
./app/db.py:14:1: E303 too many blank lines (3)
./tests/test_ping.py:4:1: F401 'app.main' imported but unused
./tests/test_summaries.py:6:1: F401 'pytest' imported but unused
./tests/test_summaries.py:60:37: F541 f-string is missing placeholders
##[error]Process completed with exit code 1.
Upvotes: 22
Views: 75904
Reputation: 2080
I got the error on my when checking my code with flake8. The problem its simply that I was using a f-string for something that could be just a string.
f-string make sense when you insert variables for example:
Correct use of f-string
age = 34
print(f'Hi my name is Enrique and my age is {age} years old')
but if you are just puting a string withou variables then you are making a wrong use of f-string
Incorrect use of f-string
print(f'Hi my name is Enrique and my age is 34 years old')
In that case you don´t need to use the f-string as you are not inserting any variable in your string
Upvotes: 17
Reputation: 81
I ran into a similar issue following the course Test-Driven Development with Django, Django REST Framework, and Docker
.
I determined that a different version of flake8
was running locally vs during my GitHub Action.
I checked locally by running (within the docker container)...
flake8 --version
...and compared that output to the logs for my GitHub Action.
In my case, flake8 3.7.9
was running in the local container, but the GitHub Action was using flake8 3.8.4
.
Looks like a new pyflakes
check for F541
made it into flake8 3.8.0
(see https://gitlab.com/pycqa/flake8/-/issues/648)
As for a fix, I see two options:
flake8 3.7.9
during the GitHub Actionfor example, in Dockerfile.prod
...
RUN pip install black flake8==3.7.9 isort
...allowed my build to finish successfully. It seems better to upgrade flake8
though and make the source comply with F541
.
I tried for awhile to understand why...
RUN pip install black flake8 isort
...wouldn't use the previously-created wheels for those packages (see Dockerfile.prod
below).
My guess is that the instruction to install and run linters during the builder
stage can't use the previously-created wheels for those linters because said wheels were built with --no-deps
. I presume that pip
falls back to searching the online index in that case and lacking other instructions installs the latest available version. Since my Dockerfile
differs materially from my Dockerfile.prod
in this area, I conclude that this gives rise to a different version of flake8
running locally vs during my GitHub Action.
Dockerfile.prod
:
#Builder
FROM python:3.8.2-alpine as builder
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONBUFFERED 1
RUN apk update \
&& apk add postgresql-dev gcc python3-dev musl-dev
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
COPY . /usr/src/app/
RUN pip install black flake8 isort
RUN flake8 .
RUN black --exclude=migrations .
RUN isort ./*/*.py
#Final
FROM python:3.8.2-alpine
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONBUFFERED 1
ENV DEBUG 0
ENV SECRET_KEY foo
ENV DJANGO_ALLOWED_HOSTS localhost 127.0.0.1 [::1] .herokuapp.com
RUN apk update \
&& apk add postgresql-dev gcc python3-dev musl-dev
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache /wheels/*
COPY . /usr/src/app/
RUN python manage.py collectstatic --noinput
RUN adduser -D myuser
RUN chown myuser /usr/src/app
USER myuser
CMD gunicorn drf_project.wsgi:application --bind 0.0.0.0:$PORT
Upvotes: 5
Reputation: 380
./app/db.py:14:1: E303 too many blank lines (3) ./tests/test_ping.py:4:1: F401 'app.main' imported but unused ./tests/test_summaries.py:6:1: F401 'pytest' imported but unused
I can see some extra error too which will again make your build fail in ci test run
From line 14 you need to remove extra line From line 4 and 6 you imported app.main and pytest which is unused in your code so need to remove that too
Upvotes: 1
Reputation: 380
f string work with place holder Example : If you want to put '/summary/' in f string assign it to some variable then put that variable in place holder
Syntax is
f'{variable}'
Example :
f'{"quoted string"}'
Upvotes: 21