[ckan-dev] imports
Toby Dacre
toby.okfn at gmail.com
Mon Mar 26 11:06:28 UTC 2012
Hi,
Just a quick email about imports (my favourite python subject)
As you probably know CKAN suffers quite badly from circular import issues.
We are going to have to live with these for a long while yet but it would
be nice to try to eventually get out of this mess.
Circular imports arise when we start using 'from' to import stuff.
Although it is really common to do this in python eg `from datetime import
datetime` it causes us problems when we start doing it __internally__
within our code.
If we import whole modules eg `import ckan.base` then this stops the
problem (when we have done this everywhere) it's fine to alias stuff eg
`import ckan.lib.helpers as h`
import * eg `from lib.base import *` is the worst possible thing we can
ever do to our codebase. This is best seen in ckan.model which we can never
import directly and we end up importing within functions which is not
ideal. Import * also makes it really hard to remove unused imports etc.
Generally it is best practice to do all our imports at the start of a
file. There are some times where we should not/cannot do this but we
should limit these. I am aware that we need to do this when
1) ckan is broken eg importing ckan.model
2) the tests break (because things get setup/run in the wrong order eg
config is not initiated) - we should fix our tests
3) when we only want to import a module if it is actually needed/used (the
caching stuff wants redis but this is not a general requirement)
If we are doing something like `import urllib` I see no justification for
importing it anywhere other than at the head of the file. importing stuff
outside the head of a file can lead to really hard to fix bugs as you can
clobber namespaces.
It is also nice to import things from the correct places eg why `from base
import _` when we could use `from pylons.i18n import _`
I am quite happy to slowly clean this stuff up as I go along but I would
really appreciate people helping to make the problem better rather than
helping to perpetuate it.
Thanks and let me know if you have any questions
Toby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20120326/da357f20/attachment.html>
More information about the ckan-dev
mailing list