Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

DM-34326 Collect imports in ConfigDictField #69

Merged
merged 8 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name: build_and_test

on:
- push
- pull_request
push:
branches:
- main
pull_request:

jobs:
build_and_test:
Expand All @@ -16,7 +18,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.8

- name: Install yaml
run: sudo apt-get install libyaml-dev
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/formatting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: pip install isort black

- name: Run isort
run: isort --check-only python/ tests/
run: isort --check-only --diff python/ tests/

- name: Run black
run: black --check --verbose --diff python/ tests/
8 changes: 5 additions & 3 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name: lint

on:
- push
- pull_request
push:
branches:
- main
pull_request:

jobs:
lint:
Expand All @@ -13,7 +15,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.8

- name: Install
run: pip install -r <(curl https://raw.githubusercontent.com/lsst/linting/main/requirements.txt)
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 21.11b1
rev: 22.3.0
hooks:
- id: black
# It is recommended to specify the latest version of Python
Expand Down
2 changes: 1 addition & 1 deletion python/lsst/pex/config/configChoiceField.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from .callStack import getCallStack, getStackFrame
from .comparison import compareConfigs, compareScalars, getComparisonName
from .config import Config, Field, FieldValidationError, _joinNamePath, _typeStr, UnexpectedProxyUsageError
from .config import Config, Field, FieldValidationError, UnexpectedProxyUsageError, _joinNamePath, _typeStr


class SelectionSet(collections.abc.MutableSet):
Expand Down
8 changes: 8 additions & 0 deletions python/lsst/pex/config/configDictField.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ def toDict(self, instance):

return dict_

def _collectImports(self, instance, imports):
# docstring inherited from Field
configDict = self.__get__(instance)
if configDict is not None:
for v in configDict.values():
v._collectImports()
imports |= v._imports

def save(self, outfile, instance):
configDict = self.__get__(instance)
fullname = _joinNamePath(instance._name, self.name)
Expand Down
2 changes: 1 addition & 1 deletion python/lsst/pex/config/configurableField.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from .callStack import getCallStack, getStackFrame
from .comparison import compareConfigs, getComparisonName
from .config import Config, Field, FieldValidationError, _joinNamePath, _typeStr, UnexpectedProxyUsageError
from .config import Config, Field, FieldValidationError, UnexpectedProxyUsageError, _joinNamePath, _typeStr


class ConfigurableInstance:
Expand Down
2 changes: 1 addition & 1 deletion python/lsst/pex/config/dictField.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
Config,
Field,
FieldValidationError,
UnexpectedProxyUsageError,
_autocast,
_joinNamePath,
_typeStr,
UnexpectedProxyUsageError,
)


Expand Down
18 changes: 7 additions & 11 deletions python/lsst/pex/config/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,13 @@ def format(config, name=None, writeSourceLine=True, prefix="", verbose=False):
for value, stack, label in config.history.get(name, []):
output = []
for frame in stack:
if (
frame.function
in (
"__new__",
"__set__",
"__setattr__",
"execfile",
"wrapper",
)
or os.path.split(frame.filename)[1] in ("argparse.py", "argumentParser.py")
):
if frame.function in (
"__new__",
"__set__",
"__setattr__",
"execfile",
"wrapper",
) or os.path.split(frame.filename)[1] in ("argparse.py", "argumentParser.py"):
if not verbose:
continue

Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where=python
[flake8]
max-line-length = 110
max-doc-length = 79
ignore = E133, E226, E228, N802, N803, N806, N812, N815, N816, W503, E203
ignore = W503, E203, E228, N802, N803, N806, N812, N815, N816
exclude =
bin,
doc,
Expand All @@ -47,4 +47,4 @@ exclude =

[tool:pytest]
addopts = --flake8
flake8-ignore = E133 E226 E228 N802 N803 N806 N812 N815 N816 W503 E203
flake8-ignore = W503 E203 N802 N803 N806 N812 N815 N816
3 changes: 1 addition & 2 deletions tests/test_configChoiceField.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ def testNoneValue(self):
self.config.validate()

def testNoPickle(self):
"""Test that pickle support is disabled for the proxy container.
"""
"""Test that pickle support is disabled for the proxy container."""
with self.assertRaises(pexConfig.UnexpectedProxyUsageError):
pickle.dumps(self.config.c)
with self.assertRaises(pexConfig.UnexpectedProxyUsageError):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_configDictField.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
class Config1(pexConfig.Config):
f = pexConfig.Field("f", float, default=3.0)

def _collectImports(self):
# Exists to test that imports of dict values are collected
self._imports.add("builtins")


class Config2(pexConfig.Config):
d1 = pexConfig.ConfigDictField("d1", keytype=str, itemtype=Config1, itemCheck=lambda x: x.f > 0)
Expand Down Expand Up @@ -114,6 +118,10 @@ def testSave(self):
c = Config2(d1={"a": Config1(f=4)})
c.save("configDictTest.py")

# verify _collectImports is called on all the configDictValues
stringOutput = c.saveToString()
self.assertIn("import builtins", stringOutput)

rt = Config2()
rt.load("configDictTest.py")

Expand Down
3 changes: 1 addition & 2 deletions tests/test_configurableField.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ def testPersistence(self):
self.assertEqual(c.c2.target, r.c2.target)

def testNoPickle(self):
"""Test that pickle support is disabled for the proxy container.
"""
"""Test that pickle support is disabled for the proxy container."""
c = Config2()
with self.assertRaises(pexConf.UnexpectedProxyUsageError):
pickle.dumps(c.c2)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_dictField.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ def testEquality(self):
self.assertTrue(pexConfig.compareConfigs("test", c1, c2))

def testNoPickle(self):
"""Test that pickle support is disabled for the proxy container.
"""
"""Test that pickle support is disabled for the proxy container."""
c = Config1()
with self.assertRaises(pexConfig.UnexpectedProxyUsageError):
pickle.dumps(c.d4)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_listField.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ def testNoArbitraryAttributes(self):
self.assertRaises(pexConfig.FieldValidationError, setattr, c.l1, "should", "fail")

def testNoPickle(self):
"""Test that pickle support is disabled for the proxy container.
"""
"""Test that pickle support is disabled for the proxy container."""
c = Config2()
with self.assertRaises(pexConfig.UnexpectedProxyUsageError):
pickle.dumps(c.ls)
Expand Down