Skip to content

bugfix: local import in spec.py#16809

Merged
alalazo merged 3 commits intospack:developfrom
skosukhin:fix_spec_import
May 27, 2020
Merged

bugfix: local import in spec.py#16809
alalazo merged 3 commits intospack:developfrom
skosukhin:fix_spec_import

Conversation

@skosukhin
Copy link
Copy Markdown
Member

Without this we have:

$ spack spec apple=banana
Input spec
--------------------------------
 apple=banana

Concretized
--------------------------------
==> Error: local variable 'spack' referenced before assignment

With this:

$ spack spec apple=banana
Input spec
--------------------------------
 apple=banana

Concretized
--------------------------------
==> Error: Attempting to concretize anonymous spec

@alalazo do we need this local import at all?

@alalazo
Copy link
Copy Markdown
Member

alalazo commented May 26, 2020

@skosukhin Local imports are usually done to optimize on startup time. Here though I don't understand what's going on, since the two forms seem to me semantically equivalent:

import foo
s = foo.func()

vs.

import foo as bar
s = bar.func()

Can you be more explicit what is the issue and why this is solving it?

@skosukhin
Copy link
Copy Markdown
Member Author

First, in the scenarios that I've checked, spack.concretize is already imported by this time. Therefore, we don't seem to get any startup time optimization here.

Second, import without as assigns the module to variable spack, which makes spack an unassigned local variable when we raise spack.error.SpecError() a little earlier in the code. We could also add global spack at the beginning of the method or perform the local import before raising the error.

@alalazo
Copy link
Copy Markdown
Member

alalazo commented May 26, 2020

@skosukhin Thanks. TIL this gotcha, that a nested import as the effect of assigning to the root module. Will search for references in Python docs to read more about that.

We could also add global spack at the beginning of the method or perform the local import before raising the error.

I would be for performing the local import at the beginning of the function. Can you add a test to check that we raise the correct exception (i.e. SpecError and not UnboundLocalError)?

@alalazo alalazo added the bugfix Something wasn't working, here's a fix label May 26, 2020
@alalazo alalazo merged commit e73c39a into spack:develop May 27, 2020
@alalazo
Copy link
Copy Markdown
Member

alalazo commented May 27, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Something wasn't working, here's a fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants