Skip to content

Commit

Permalink
Use "is" instead of "==" in assert in serialized file
Browse files Browse the repository at this point in the history
This required a change to the regex that parses the class name
on read.

Also changed the code to use f-string.
  • Loading branch information
timj committed Jul 11, 2024
1 parent e2593c3 commit a57aed8
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions python/lsst/pex/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1375,9 +1375,12 @@ def saveToStream(self, outfile, root="config", skipImports=False):
configType = type(self)
typeString = _typeStr(configType)
outfile.write(f"import {configType.__module__}\n")
# We are required to write this on a single line because
# of later regex matching, rather than adopting black style
# formatting.
outfile.write(
f"assert type({root})=={typeString}, 'config is of type %s.%s instead of "
f"{typeString}' % (type({root}).__module__, type({root}).__name__)\n"
f'assert type({root}) is {typeString}, f"config is of type '
f'{{type({root}).__module__}}.{{type({root}).__name__}} instead of {typeString}"\n\n'
)
for imp in sorted(self._imports):
if imp in sys.modules and sys.modules[imp] is not None:
Expand Down Expand Up @@ -1708,12 +1711,15 @@ def _classFromPython(config_py):
"""
# standard serialization has the form:
# import config.class
# assert type(config)==config.class.Config, ...
# assert type(config) is config.class.Config, ...
# Older files use "type(config)==" instead.
# We want to parse these two lines so we can get the class itself

# Do a single regex to avoid large string copies when splitting a
# large config into separate lines.
matches = re.search(r"^import ([\w.]+)\nassert .*==(.*?),", config_py)
# The assert regex cannot be greedy because the assert error string
# can include both "," and " is ".
matches = re.search(r"^import ([\w.]+)\nassert type\(\S+\)(?:\s*==\s*| is )(.*?),", config_py)

if not matches:
first_line, second_line, _ = config_py.split("\n", 2)
Expand Down

0 comments on commit a57aed8

Please sign in to comment.