Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self: "Env", name: str, default: typing.Any = ma.missing, subcast: Subcast = None, **kwargs
) -> _T:
if self._sealed:
raise EnvSealedError("Env has already been sealed. New values cannot be parsed.")
missing = kwargs.pop("missing", None) or default
if isinstance(field_or_factory, type) and issubclass(field_or_factory, ma.fields.Field):
field = field_or_factory(missing=missing, **kwargs)
else:
field = field_or_factory(subcast=subcast, missing=missing, **kwargs)
parsed_key, raw_value, proxied_key = self._get_from_environ(name, ma.missing)
self._fields[parsed_key] = field
source_key = proxied_key or parsed_key
if raw_value is ma.missing and field.missing is ma.missing:
message = "Environment variable not set."
if self.eager:
raise EnvValidationError('Environment variable "{}" not set'.format(source_key), [message])
else:
self._errors[parsed_key].append(message)
if raw_value or raw_value == "":
value = raw_value
else:
value = field.missing
if preprocess:
value = preprocess(value, subcast=subcast, **kwargs)
try:
value = field.deserialize(value)
except ma.ValidationError as error:
if self.eager:
raise EnvValidationError(
'Environment variable "{}" invalid: {}'.format(source_key, error.args[0]), error.messages
) from error
self._errors[parsed_key].extend(error.messages)
message = "Environment variable not set."
if self.eager:
raise EnvValidationError('Environment variable "{}" not set'.format(source_key), [message])
else:
self._errors[parsed_key].append(message)
if raw_value or raw_value == "":
value = raw_value
else:
value = field.missing
if preprocess:
value = preprocess(value, subcast=subcast, **kwargs)
try:
value = field.deserialize(value)
except ma.ValidationError as error:
if self.eager:
raise EnvValidationError(
'Environment variable "{}" invalid: {}'.format(source_key, error.args[0]), error.messages
) from error
self._errors[parsed_key].extend(error.messages)
else:
self._values[parsed_key] = value
return value
def seal(self):
"""Validate parsed values and prevent new values from being added.
:raises: environs.EnvValidationError
"""
self._sealed = True
if self._errors:
error_messages = dict(self._errors)
self._errors = {}
raise EnvValidationError(
"Environment variables invalid: {}".format(error_messages), error_messages
)