Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

tests/base_test.py: Use __name__ for Flask's import_name #74

Merged
merged 1 commit into from
Sep 19, 2014

Conversation

wking
Copy link
Contributor

@wking wking commented Sep 19, 2014

Avoid several test failures like:

======================================================================
ERROR: test_credentialed_request (tests.test_credentials.AppConfigExposeHeadersTestCase)
AppConfigExposeHeadersTestCase.test_credentialed_request
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/.../tests/base_test.py", line 82, in setUp
    self.app = Flask(self.__class__.__name__)
  File "/usr/lib64/python3.4/site-packages/flask/app.py", line 319, in __init__
    template_folder=template_folder)
  File "/usr/lib64/python3.4/site-packages/flask/helpers.py", line 741, in __init__
    self.root_path = get_root_path(self.import_name)
  File "/usr/lib64/python3.4/site-packages/flask/helpers.py", line 631, in get_root_path
    loader = pkgutil.get_loader(import_name)
  File "/usr/lib64/python3.4/pkgutil.py", line 469, in get_loader
    return find_loader(fullname)
  File "/usr/lib64/python3.4/pkgutil.py", line 490, in find_loader
    return spec.loader
AttributeError: 'NoneType' object has no attribute 'loader'

on Python 3.4 1. The solution is to use an importable name 2:

About the First Parameter

The idea of the first parameter is to give Flask an idea what
belongs to your application. This name is used to find resources on
the file system, can be used by extensions to improve debugging
information and a lot more.

So it’s important what you provide there. If you are using a single
module, name is always the correct value. If you however are
using a package, it’s usually recommended to hardcode the name of
your package there.

Previously we were using the class name (not the module name), but you
can't use that to import anything ;).

Avoid several test failures like:

  ======================================================================
  ERROR: test_credentialed_request (tests.test_credentials.AppConfigExposeHeadersTestCase)
  AppConfigExposeHeadersTestCase.test_credentialed_request
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "/.../tests/base_test.py", line 82, in setUp
      self.app = Flask(self.__class__.__name__)
    File "/usr/lib64/python3.4/site-packages/flask/app.py", line 319, in __init__
      template_folder=template_folder)
    File "/usr/lib64/python3.4/site-packages/flask/helpers.py", line 741, in __init__
      self.root_path = get_root_path(self.import_name)
    File "/usr/lib64/python3.4/site-packages/flask/helpers.py", line 631, in get_root_path
      loader = pkgutil.get_loader(import_name)
    File "/usr/lib64/python3.4/pkgutil.py", line 469, in get_loader
      return find_loader(fullname)
    File "/usr/lib64/python3.4/pkgutil.py", line 490, in find_loader
      return spec.loader
  AttributeError: 'NoneType' object has no attribute 'loader'

on Python 3.4 [1].  The solution is to use an importable name [2]:

> About the First Parameter
>
> The idea of the first parameter is to give Flask an idea what
> belongs to your application. This name is used to find resources on
> the file system, can be used by extensions to improve debugging
> information and a lot more.
>
> So it’s important what you provide there. If you are using a single
> module, __name__ is always the correct value. If you however are
> using a package, it’s usually recommended to hardcode the name of
> your package there.

Previously we were using the class name (not the module name), but you
can't use that to import anything ;).

[1]: pallets/flask#1011
[2]: http://flask.pocoo.org/docs/0.10/api/#flask.Flask
@corydolphin
Copy link
Owner

Great!, looks good to me. I'll cut a patch release tonight.

@coveralls
Copy link

Coverage Status

Coverage remained the same when pulling 2a8f9c5 on wking:flask-import-name into 4345861 on wcdolphin:master.

@idella
Copy link

idella commented Sep 19, 2014

thanks, mate

corydolphin pushed a commit that referenced this pull request Sep 19, 2014
tests/base_test.py: Use __name__ for Flask's import_name
@corydolphin corydolphin merged commit 7f1e841 into corydolphin:master Sep 19, 2014
@corydolphin
Copy link
Owner

Just to close the loop, I had a few changes pending and wrapped them in for a dot release.

Shipped 1.9.0 earlier this morning.

@wking wking deleted the flask-import-name branch September 22, 2014 12:51
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants