Skip to content

Commit

Permalink
Remove unused validator functionality and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fkglr committed Oct 23, 2024
1 parent a41a699 commit 3476ebd
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 196 deletions.
6 changes: 3 additions & 3 deletions tests/bats/run_bats.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

set -eu

gallia script vecu --no-volatile-info "unix-lines:///tmp/vecu.sock" rng \
gallia script vecu --no-volatile-info rng "unix-lines:///tmp/vecu.sock" \
--seed 3 \
--mandatory_sessions "[1, 2, 3]" \
--mandatory_services "[DiagnosticSessionControl, EcuReset, ReadDataByIdentifier, WriteDataByIdentifier, RoutineControl, SecurityAccess, ReadMemoryByAddress, WriteMemoryByAddress, RequestDownload, RequestUpload, TesterPresent, ReadDTCInformation, ClearDiagnosticInformation, InputOutputControlByIdentifier]" 2>vecu.log &
--mandatory_sessions "1 2 3" \
--mandatory_services "DiagnosticSessionControl EcuReset ReadDataByIdentifier WriteDataByIdentifier RoutineControl SecurityAccess ReadMemoryByAddress WriteMemoryByAddress RequestDownload RequestUpload TesterPresent ReadDTCInformation ClearDiagnosticInformation InputOutputControlByIdentifier" 2>vecu.log &

# https://superuser.com/a/553236
trap 'kill "$(jobs -p)"' SIGINT SIGTERM EXIT
Expand Down
20 changes: 6 additions & 14 deletions vendor/pydantic-argparse/pydantic_argparse/argparse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import argparse
import sys
from typing import Dict, Generic, List, NoReturn, Optional, Tuple, Type, Any
from typing import Generic, List, NoReturn, Optional, Tuple, Type, Any

from pydantic import BaseModel, ValidationError

from pydantic_argparse import parsers, utils
from pydantic_argparse import parsers
from pydantic_argparse.argparse import actions
from pydantic_argparse.utils.field import ArgFieldInfo
from pydantic_argparse.utils.nesting import _NestedArgumentParser
Expand Down Expand Up @@ -301,7 +301,6 @@ def _add_model(
Type[PydanticModelT]: Pydantic model possibly with new validators.
"""
# Initialise validators dictionary
validators: Dict[str, utils.pydantic.PydanticValidator] = dict()
parser = self if arg_group is None else arg_group

explicit_groups = {}
Expand All @@ -310,7 +309,7 @@ def _add_model(
for field in PydanticField.parse_model(model):
if field.is_a(BaseModel):
if field.is_subcommand():
validator = parsers.command.parse_field(self._commands(), field, self.extra_defaults)
parsers.command.parse_field(self._commands(), field, self.extra_defaults)
else:
# for any nested pydantic models, set default factory to model_construct
# method. This allows pydantic to handle if no arguments from a nested
Expand All @@ -325,9 +324,6 @@ def _add_model(

# recurse and parse fields below this submodel
self._add_model(model=field.model_type, arg_group=arg_group)

validator = None

else:
# Add field
added = False
Expand All @@ -346,14 +342,10 @@ def _add_model(
if field.info.group not in explicit_groups:
explicit_groups[field.info.group] = self.add_argument_group(field.info.group)

validator = parsers.add_field(explicit_groups[field.info.group], field)
parsers.add_field(explicit_groups[field.info.group], field)
added = True

if not added:
validator = parsers.add_field(parser, field)

# Update validators
utils.pydantic.update_validators(validators, validator)
parsers.add_field(parser, field)

# Construct and return model with validators
return utils.pydantic.model_with_validators(model, validators)
return model
27 changes: 10 additions & 17 deletions vendor/pydantic-argparse/pydantic_argparse/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,26 @@
from .utils import SupportsAddArgument


# TODO: The validators do not work for nested models, either fix or remove this functionality
def add_field(
parser: SupportsAddArgument,
field: PydanticField,
) -> Optional[PydanticValidator]:
) -> None:
"""Parses pydantic field type, and then adds it to argument parser.
Args:
parser (argparse.ArgumentParser | argparse._ArgumentGroup): Sub-parser to add to.
field (pydantic.fields.ModelField): Field to be added to parser.
Returns:
Optional[utils.pydantic.PydanticValidator]: Possible validator method.
"""
# Switch on Field Type -- for fields that are pydantic models
# this gets handled at the top level to distinguish
# subcommands from arg groups
if boolean.should_parse(field):
return boolean.parse_field(parser, field)

if container.should_parse(field):
return container.parse_field(parser, field)

if literal.should_parse(field):
return literal.parse_field(parser, field)

if enum.should_parse(field):
return enum.parse_field(parser, field)

return standard.parse_field(parser, field)
boolean.parse_field(parser, field)
elif container.should_parse(field):
container.parse_field(parser, field)
elif literal.should_parse(field):
literal.parse_field(parser, field)
elif enum.should_parse(field):
enum.parse_field(parser, field)
else:
standard.parse_field(parser, field)
12 changes: 2 additions & 10 deletions vendor/pydantic-argparse/pydantic_argparse/parsers/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
command-line arguments.
"""

import argparse
from typing import Optional

from pydantic_argparse import utils
from pydantic_argparse.argparse import actions
from pydantic_argparse.utils.pydantic import PydanticField, PydanticValidator
from pydantic_argparse.utils.pydantic import PydanticField

from .utils import SupportsAddArgument

Expand All @@ -36,15 +33,12 @@ def should_parse(field: PydanticField) -> bool:
def parse_field(
parser: SupportsAddArgument,
field: PydanticField,
) -> Optional[PydanticValidator]:
) -> None:
"""Adds boolean pydantic field to argument parser.
Args:
parser (argparse.ArgumentParser): Argument parser to add to.
field (PydanticField): Field to be added to parser.
Returns:
Optional[PydanticValidator]: Possible validator method.
"""
# Determine Argument Properties
action = actions.BooleanOptionalAction
Expand All @@ -59,5 +53,3 @@ def parse_field(
**field.arg_const(),
**field.arg_dest(),
)

return None
12 changes: 3 additions & 9 deletions vendor/pydantic-argparse/pydantic_argparse/parsers/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
"""

import argparse
from typing import Optional, Type, Any
from typing import Type, Any

from pydantic_argparse.utils.pydantic import (
PydanticField,
PydanticValidator, as_validator,
)


Expand All @@ -36,23 +35,18 @@ def parse_field(
subparser: argparse._SubParsersAction,
field: PydanticField,
extra_defaults: dict[Type, dict[str, Any]] | None = None,
) -> Optional[PydanticValidator]:
) -> None:
"""Adds command pydantic field to argument parser.
Args:
subparser (argparse._SubParsersAction): Sub-parser to add to.
field (PydanticField): Field to be added to parser.
Returns:
Optional[PydanticValidator]: Possible validator method.
"""
# Add Command
sp = subparser.add_parser(
subparser.add_parser(
field.info.title or field.info.alias or field.name,
help=field.info.description,
model=field.model_type, # type: ignore[call-arg]
exit_on_error=False, # Allow top level parser to handle exiting
extra_defaults=extra_defaults,
)

return as_validator(field, lambda x: sp.model.__pydantic_validator__.validate_python(x), silent=False, expected_input_type=dict)
11 changes: 2 additions & 9 deletions vendor/pydantic-argparse/pydantic_argparse/parsers/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import argparse
import collections.abc
import enum
from typing import Optional

from pydantic_argparse import utils
from pydantic_argparse.utils.pydantic import PydanticField, PydanticValidator
from pydantic_argparse.utils.pydantic import PydanticField

from .utils import SupportsAddArgument

Expand All @@ -37,15 +35,12 @@ def should_parse(field: PydanticField) -> bool:
def parse_field(
parser: SupportsAddArgument,
field: PydanticField,
) -> Optional[PydanticValidator]:
) -> None:
"""Adds container pydantic field to argument parser.
Args:
parser (argparse.ArgumentParser): Argument parser to add to.
field (PydanticField): Field to be added to parser.
Returns:
Optional[PydanticValidator]: Possible validator method.
"""
parser.add_argument(
*field.arg_names(),
Expand All @@ -59,5 +54,3 @@ def parse_field(
**field.arg_dest(),
)

return None

13 changes: 4 additions & 9 deletions vendor/pydantic-argparse/pydantic_argparse/parsers/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@

import argparse
import enum
from typing import Optional, Type, cast, TypeVar
from typing import Type, cast

from pydantic_argparse import utils
from pydantic_argparse.utils.pydantic import PydanticField, PydanticValidator
from pydantic_argparse.utils.pydantic import PydanticField

from .utils import SupportsAddArgument

Expand All @@ -36,20 +35,18 @@ def should_parse(field: PydanticField) -> bool:
def parse_field(
parser: SupportsAddArgument,
field: PydanticField,
) -> Optional[PydanticValidator]:
) -> None:
"""Adds enum pydantic field to argument parser.
Args:
parser (argparse.ArgumentParser): Argument parser to add to.
field (PydanticField): Field to be added to parser.
Returns:
Optional[PydanticValidator]: Possible validator method.
"""
# Extract Enum
enum_type = cast(Type[enum.Enum], field.info.annotation)

# Determine Argument Properties
# TODO: Short metavar
metavar = f"{{{', '.join(f'{e.value}' for e in enum_type)}}}"
action = argparse._StoreAction

Expand All @@ -64,5 +61,3 @@ def parse_field(
**field.arg_const(),
**field.arg_dest(),
)

return None
12 changes: 2 additions & 10 deletions vendor/pydantic-argparse/pydantic_argparse/parsers/literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@

import argparse
import sys
from enum import Enum
from typing import Optional

from pydantic_argparse import utils
from pydantic_argparse.utils.pydantic import PydanticField, PydanticValidator
from pydantic_argparse.utils.pydantic import PydanticField

from .utils import SupportsAddArgument

Expand All @@ -42,15 +39,12 @@ def should_parse(field: PydanticField) -> bool:
def parse_field(
parser: SupportsAddArgument,
field: PydanticField,
) -> Optional[PydanticValidator]:
) -> None:
"""Adds enum pydantic field to argument parser.
Args:
parser (argparse.ArgumentParser): Argument parser to add to.
field (PydanticField): Field to be added to parser.
Returns:
Optional[PydanticValidator]: Possible validator method.
"""
# Extract Choices
choices = get_args(field.info.annotation)
Expand All @@ -70,5 +64,3 @@ def parse_field(
**field.arg_const(),
**field.arg_dest(),
)

return None
12 changes: 2 additions & 10 deletions vendor/pydantic-argparse/pydantic_argparse/parsers/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,21 @@
"""

import argparse
from typing import Optional

from pydantic_argparse import utils
from pydantic_argparse.utils.pydantic import PydanticField, PydanticValidator
from pydantic_argparse.utils.pydantic import PydanticField

from .utils import SupportsAddArgument


def parse_field(
parser: SupportsAddArgument,
field: PydanticField,
) -> Optional[PydanticValidator]:
) -> None:
"""Adds standard pydantic field to argument parser.
Args:
parser (argparse.ArgumentParser): Argument parser to add to.
field (PydanticField): Field to be added to parser.
Returns:
Optional[PydanticValidator]: Possible validator method.
"""
# Add Standard Field
parser.add_argument(
Expand All @@ -45,6 +40,3 @@ def parse_field(
**field.arg_const(),
**field.arg_dest(),
)

# Construct and Return Validator
return None
Loading

0 comments on commit 3476ebd

Please sign in to comment.