5

So Pylint (1.4.3) is reporting a Cyclic Import and it doesn't make much sense. First of all, the file that's report has no import statements.

Second of all no files import the reference file. a __init__.py file loads configuration values from development_config (file in question) but no files import said file.

So why is Pylint giving me this warning?

Pylint warning

************* Module heart_beat.development_config
R:  1, 0: Cyclic import (heart_beat -> heart_beat.views -> heart_beat.models) (cyclic-import)
R:  1, 0: Cyclic import (heart_beat -> heart_beat.views) (cyclic-import)

development_config

""" -------------------------- DATA BASE CONFINGURATION --------------------"""
SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db'
SQLALCHEMY_ECHO = False

""" -------------------------- Flask Application Config --------------------"""
THREADS_PER_PAGE = 8
VERSION = "0.1"

__init__.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

#from register_assets import register_all

app = Flask(__name__, static_url_path='/static')

# the environment variable LIMBO_SETTINGS is set in runserver, run_unit_tests
# or limbo.wsgi.

def load_configs():
    """Take all configs found in development_config.py."""
    app.config.from_pyfile("development_config.py", silent=False)

load_configs()

# global SQLAlchemy configuration
db = SQLAlchemy(app)

#Create and register all static asset bundles.
#register_all(app)

#NOTE: DON'T LISTEN TO YOUR IDE! heart_beat.views is used and required.
import heart_beat.views  # views contains all URL routes, Importing sets routes.
def setup_db():
    """Database creation in a file rather then a statement for easier tests."""
    db.create_all()

def teardown_db():
    """Database deletion in a file rather then a statement for easier tests."""
    db.drop_all()

setup_db()

views.py

from flask import request

from . import app
from . import db
from . import models
from . import exceptions as ex

models.py

import datetime

from . import exceptions
from . import db
from . import app
1
  • 1
    I'm not sure why it's calling out development_config specifically, but you do have circular imports for sure (app (__init__.py) imports views, views imports app). Generally in flask one would use blueprints to define views in separate files. This large flask project has really good organization, it's worth looking at: github.com/masom/bluemonk
    – engineerC
    Commented Jun 5, 2015 at 1:37

1 Answer 1

16

I believe this is currently a bug in pylint. Things that require analysis over multiple modules (for example cyclic-import and duplicate-code detection) get thrown as refactors into the last-parsed module file. For me, this ended up being an empty __init__.py file that had both of these dropped into it.

Both of these refactor messages though contain the actual module names which are problematic:

  • For cyclic-import, the problematic modules are listed in the parenthesis
  • For duplicate-code, the problematic modules are listed on the following lines starting with ==

The grouping of these is not limited to the print-out of modules, it also effects the summary report % errors / warnings by module in which that final parsed file gets the counts for the refactors and none of the modules it actually concerns get any counts from them.

1
  • 4
    Is there anyway to disable those false positives without totally disabling cyclic-import and duplicate-code?
    – adamyi
    Commented Mar 31, 2019 at 6:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.