Skip to content

Commit 2aa0de9

Browse files
flake8 clean and use pytest parametrize
1 parent 1857365 commit 2aa0de9

File tree

6 files changed

+71
-41
lines changed

6 files changed

+71
-41
lines changed

.hgignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ syntax:glob
1313
build
1414
dist
1515
apipkg.egg-info
16-
16+
.cache/
17+
.eggs/

apipkg.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
__version__ = '1.3.dev'
1313

14+
1415
def _py_abspath(path):
1516
"""
1617
special version of abspath
@@ -22,6 +23,7 @@ def _py_abspath(path):
2223
else:
2324
return os.path.abspath(path)
2425

26+
2527
def initpkg(pkgname, exportdefs, attr=dict()):
2628
""" initialize given package from the export definitions. """
2729
oldmod = sys.modules.get(pkgname)
@@ -44,6 +46,7 @@ def initpkg(pkgname, exportdefs, attr=dict()):
4446
mod = ApiModule(pkgname, exportdefs, implprefix=pkgname, attr=d)
4547
sys.modules[pkgname] = mod
4648

49+
4750
def importobj(modpath, attrname):
4851
module = __import__(modpath, None, None, ['__doc__'])
4952
if not attrname:
@@ -55,13 +58,15 @@ def importobj(modpath, attrname):
5558
retval = getattr(retval, x)
5659
return retval
5760

61+
5862
class ApiModule(ModuleType):
5963
def __docget(self):
6064
try:
6165
return self.__doc
6266
except AttributeError:
6367
if '__doc__' in self.__map__:
6468
return self.__makeattr('__doc__')
69+
6570
def __docset(self, value):
6671
self.__doc = value
6772
__doc__ = property(__docget, __docset)
@@ -132,8 +137,10 @@ def __makeattr(self, name):
132137

133138
__getattr__ = __makeattr
134139

140+
@property
135141
def __dict__(self):
136-
# force all the content of the module to be loaded when __dict__ is read
142+
# force all the content of the module
143+
# to be loaded when __dict__ is read
137144
dictdescr = ModuleType.__dict__['__dict__']
138145
dict = dictdescr.__get__(self)
139146
if dict is not None:
@@ -144,7 +151,6 @@ def __dict__(self):
144151
except AttributeError:
145152
pass
146153
return dict
147-
__dict__ = property(__dict__)
148154

149155

150156
def AliasModule(modname, modpath, attrname=None):

conftest.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

setup.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@
66
(c) 2009 holger krekel, Holger Krekel
77
"""
88

9-
import os, sys
9+
from setuptools import setup
1010

11-
try:
12-
from setuptools import setup
13-
except ImportError:
14-
from distutils.core import setup
1511

1612
def main():
1713
setup(
1814
name='apipkg',
19-
description=
20-
'apipkg: namespace control and lazy-import mechanism',
21-
long_description = open('README.txt').read(),
22-
version='1.3.dev',
15+
description='apipkg: namespace control and lazy-import mechanism',
16+
long_description=open('README.txt').read(),
17+
get_version_from_scm=True,
2318
url='http://bitbucket.org/hpk42/apipkg',
2419
license='MIT License',
2520
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],
@@ -34,8 +29,11 @@ def main():
3429
'Operating System :: MacOS :: MacOS X',
3530
'Topic :: Software Development :: Libraries',
3631
'Programming Language :: Python'],
37-
py_modules=['apipkg']
32+
py_modules=['apipkg'],
33+
setup_requires=[
34+
'hgdistver'
35+
]
3836
)
3937

4038
if __name__ == '__main__':
41-
main()
39+
main()

test_apipkg.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import py
44
import apipkg
55
import subprocess
6+
import pytest
67
#
78
# test support for importing modules
89
#
9-
ModuleType = py.std.types.ModuleType
10+
ModuleType = types.ModuleType
11+
1012

1113
class TestRealModule:
1214

@@ -61,7 +63,7 @@ def test_realmodule(self):
6163

6264
def test_realmodule_repr(self):
6365
import realtest.x
64-
assert "<ApiModule 'realtest.x'>" == repr(realtest.x)
66+
assert "<ApiModule 'realtest.x'>" == repr(realtest.x)
6567

6668
def test_realmodule_from(self):
6769
from realtest.x import module
@@ -86,6 +88,7 @@ def test_realmodule___doc__(self):
8688
print (realtest.x.module.__map__)
8789
assert realtest.x.module.__doc__ == 'test module'
8890

91+
8992
class TestScenarios:
9093
def test_relative_import(self, monkeypatch, tmpdir):
9194
pkgdir = tmpdir.mkdir("mymodule")
@@ -150,9 +153,9 @@ def test_from_module_alias_import(self, monkeypatch, tmpdir):
150153
from fromaliasimport.some import join
151154
assert join is py.std.os.path.join
152155

156+
153157
def xtest_nested_absolute_imports():
154-
import email
155-
api_email = apipkg.ApiModule('email',{
158+
apipkg.ApiModule('email', {
156159
'message2': {
157160
'Message': 'email.message:Message',
158161
},
@@ -162,6 +165,8 @@ def xtest_nested_absolute_imports():
162165

163166
# alternate ideas for specifying package + preliminary code
164167
#
168+
169+
165170
def test_parsenamespace():
166171
spec = """
167172
path.local __.path.local::LocalPath
@@ -170,10 +175,14 @@ def test_parsenamespace():
170175
"""
171176
d = parsenamespace(spec)
172177
print (d)
173-
assert d == {'test': {'raises': '__.test.outcome::raises'},
174-
'path': {'svnwc': '__.path.svnwc::WCCommandPath',
175-
'local': '__.path.local::LocalPath'}
176-
}
178+
assert d == {
179+
'test': {'raises': '__.test.outcome::raises'},
180+
'path': {
181+
'svnwc': '__.path.svnwc::WCCommandPath',
182+
'local': '__.path.local::LocalPath'}
183+
}
184+
185+
177186
def xtest_parsenamespace_errors():
178187
py.test.raises(ValueError, """
179188
parsenamespace('path.local xyz')
@@ -182,6 +191,7 @@ def xtest_parsenamespace_errors():
182191
parsenamespace('x y z')
183192
""")
184193

194+
185195
def parsenamespace(spec):
186196
ns = {}
187197
for line in spec.split("\n"):
@@ -190,10 +200,10 @@ def parsenamespace(spec):
190200
continue
191201
parts = [x.strip() for x in line.split()]
192202
if len(parts) != 2:
193-
raise ValueError("Wrong format: %r" %(line,))
203+
raise ValueError("Wrong format: %r" % (line,))
194204
apiname, spec = parts
195205
if not spec.startswith("__"):
196-
raise ValueError("%r does not start with __" %(spec,))
206+
raise ValueError("%r does not start with __" % (spec,))
197207
apinames = apiname.split(".")
198208
cur = ns
199209
for name in apinames[:-1]:
@@ -202,6 +212,7 @@ def parsenamespace(spec):
202212
cur[apinames[-1]] = spec
203213
return ns
204214

215+
205216
def test_initpkg_replaces_sysmodules(monkeypatch):
206217
mod = ModuleType('hello')
207218
monkeypatch.setitem(sys.modules, 'hello', mod)
@@ -210,6 +221,7 @@ def test_initpkg_replaces_sysmodules(monkeypatch):
210221
assert newmod != mod
211222
assert newmod.x == py.std.os.path.abspath
212223

224+
213225
def test_initpkg_transfers_attrs(monkeypatch):
214226
mod = ModuleType('hello')
215227
mod.__version__ = 10
@@ -225,6 +237,7 @@ def test_initpkg_transfers_attrs(monkeypatch):
225237
assert newmod.__loader__ == mod.__loader__
226238
assert newmod.__doc__ == mod.__doc__
227239

240+
228241
def test_initpkg_nodoc(monkeypatch):
229242
mod = ModuleType('hello')
230243
mod.__file__ = "hello.py"
@@ -233,6 +246,7 @@ def test_initpkg_nodoc(monkeypatch):
233246
newmod = sys.modules['hello']
234247
assert not newmod.__doc__
235248

249+
236250
def test_initpkg_overwrite_doc(monkeypatch):
237251
hello = ModuleType('hello')
238252
hello.__doc__ = "this is the documentation"
@@ -242,6 +256,7 @@ def test_initpkg_overwrite_doc(monkeypatch):
242256
assert newhello != hello
243257
assert newhello.__doc__ == sys.__doc__
244258

259+
245260
def test_initpkg_not_transfers_not_existing_attrs(monkeypatch):
246261
mod = ModuleType('hello')
247262
mod.__file__ = "hello.py"
@@ -273,16 +288,18 @@ def test_initpkg_defaults(monkeypatch):
273288
monkeypatch.setitem(sys.modules, 'hello', mod)
274289
apipkg.initpkg('hello', {})
275290
newmod = sys.modules['hello']
276-
assert newmod.__file__ == None
291+
assert newmod.__file__ is None
277292
assert not hasattr(newmod, '__version__')
278293

294+
279295
def test_name_attribute():
280296
api = apipkg.ApiModule('name_test', {
281297
'subpkg': {},
282298
})
283299
assert api.__name__ == 'name_test'
284300
assert api.subpkg.__name__ == 'name_test.subpkg'
285301

302+
286303
def test_error_loading_one_element(monkeypatch, tmpdir):
287304
pkgdir = tmpdir.mkdir("errorloading1")
288305
pkgdir.join('__init__.py').write(py.code.Source("""
@@ -301,6 +318,7 @@ def test_error_loading_one_element(monkeypatch, tmpdir):
301318
py.test.raises(ImportError, 'errorloading1.x')
302319
py.test.raises(ImportError, 'errorloading1.x')
303320

321+
304322
def test_onfirstaccess(tmpdir, monkeypatch):
305323
pkgdir = tmpdir.mkdir("firstaccess")
306324
pkgdir.join('__init__.py').write(py.code.Source("""
@@ -323,7 +341,8 @@ def init():
323341
assert len(firstaccess.l) == 1
324342
assert '__onfirstaccess__' not in firstaccess.__all__
325343

326-
@py.test.mark.multi(mode=['attr', 'dict', 'onfirst'])
344+
345+
@pytest.mark.parametrize('mode', ['attr', 'dict', 'onfirst'])
327346
def test_onfirstaccess_setsnewattr(tmpdir, monkeypatch, mode):
328347
pkgname = 'mode_' + mode
329348
pkgdir = tmpdir.mkdir(pkgname)
@@ -352,6 +371,7 @@ def init():
352371
assert not hasattr(mod, '__onfirstaccess__')
353372
assert '__onfirstaccess__' not in vars(mod)
354373

374+
355375
def test_bpython_getattr_override(tmpdir, monkeypatch):
356376
def patchgetattr(self, name):
357377
raise AttributeError(name)
@@ -363,12 +383,10 @@ def patchgetattr(self, name):
363383
assert 'abspath' in d
364384

365385

366-
367-
368386
def test_chdir_with_relative_imports_shouldnt_break_lazy_loading(tmpdir):
369387
tmpdir.join('apipkg.py').write(py.code.Source(apipkg))
370388
pkg = tmpdir.mkdir('pkg')
371-
messy = tmpdir.mkdir('messy')
389+
tmpdir.mkdir('messy')
372390
pkg.join('__init__.py').write(py.code.Source("""
373391
import apipkg
374392
apipkg.initpkg(__name__, {
@@ -408,6 +426,7 @@ def test_dotted_name_lookup(tmpdir, monkeypatch):
408426
import dotted_name_lookup
409427
assert dotted_name_lookup.abs == py.std.os.path.abspath
410428

429+
411430
def test_extra_attributes(tmpdir, monkeypatch):
412431
pkgdir = tmpdir.mkdir("extra_attributes")
413432
pkgdir.join('__init__.py').write(py.code.Source("""
@@ -418,30 +437,35 @@ def test_extra_attributes(tmpdir, monkeypatch):
418437
import extra_attributes
419438
assert extra_attributes.foo == 'bar'
420439

440+
421441
def test_aliasmodule_aliases_an_attribute():
422442
am = apipkg.AliasModule("mymod", "pprint", 'PrettyPrinter')
423443
r = repr(am)
424444
assert "<AliasModule 'mymod' for 'pprint.PrettyPrinter'>" == r
425445
assert am.format
426446
assert not hasattr(am, "lqkje")
427447

448+
428449
def test_aliasmodule_aliases_unimportable():
429450
am = apipkg.AliasModule("mymod", "qlwkejqlwe", 'main')
430451
r = repr(am)
431452
assert "<AliasModule 'mymod' for 'qlwkejqlwe.main'>" == r
432453
assert am.qwe is None
433454

455+
434456
def test_aliasmodule_unicode():
435457
am = apipkg.AliasModule(py.builtin._totext("mymod"), "pprint")
436458
assert am
437459

460+
438461
def test_aliasmodule_repr():
439462
am = apipkg.AliasModule("mymod", "sys")
440463
r = repr(am)
441464
assert "<AliasModule 'mymod' for 'sys'>" == r
442465
am.version
443466
assert repr(am) == r
444467

468+
445469
def test_aliasmodule_proxy_methods(tmpdir, monkeypatch):
446470
pkgdir = tmpdir
447471
pkgdir.join('aliasmodule_proxy.py').write(py.code.Source("""
@@ -470,6 +494,7 @@ def doit():
470494
proxy.doit = doit
471495
assert orig.doit is doit
472496

497+
473498
def test_aliasmodule_nested_import_with_from(tmpdir, monkeypatch):
474499
import os
475500
pkgdir = tmpdir.mkdir("api1")

tox.ini

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
[tox]
2-
envlist=py27,py26,py33,py34,jython
2+
envlist=py27,py26,py33,py34,jython,flakes
33

44
[tox:hudson]
55
sdistsrc={distshare}/apipkg-*
66

77
[testenv]
8-
commands=py.test []
98
deps=pytest
9+
commands=py.test []
1010

1111
[testenv:jython]
12+
deps=pytest
1213
commands=py.test-jython []
14+
15+
[testenv:flakes]
16+
deps=flake8
17+
commands=flake8
18+
19+
20+
[flake8]
21+
exclude=.tox/.env/,dist/,build/,example/
22+
max_complexity=11

0 commit comments

Comments
 (0)