-
Notifications
You must be signed in to change notification settings - Fork 81
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
Reduced the type: ignore comments and allocated the structured dataclass #393
base: main
Are you sure you want to change the base?
Reduced the type: ignore comments and allocated the structured dataclass #393
Conversation
odxtools/cli/compare.py
Outdated
|
||
# name of the tool | ||
_odxtools_tool_name_ = "compare" | ||
|
||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commented out code
odxtools/cli/compare.py
Outdated
deleted_variants : List[str] | ||
service_changes : SpecsServiceDict | ||
#class extract_service_tabulation_data(TypedDict): | ||
# table: RichTable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commented out code
odxtools/cli/compare.py
Outdated
"changed_name_of_service"][0] or service_dict["changed_parameters_of_service"][0]: | ||
assert isinstance(service_dict["diag_layer"], str) | ||
"changed_name_of_service"] or service_dict["changed_parameters_of_service"]: | ||
# assert isinstance(service_dict["diag_layer"], str) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commented out code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In principle, I like it. IMO -- besides the usual bits and pieces that need to be fixed -- for whatever reason it seems that a lot of unrelated formatting changes slipped into this (which makes reviewing it more difficult than it needs to be and also prevents the CI from passing). To fix the formatting, install yapf
and isort
via pip and run the reformat-source.sh script in the toplevel directory of your working copy...
odxtools/cli/compare.py
Outdated
|
||
SpecsServiceDict = Dict[str, Union[VariantName, VariantType, NewServices, DeletedServices, | ||
RenamedServices, ServicesWithParamChanges]] | ||
class SpecsServiceDict(TypedDict): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO that should better be an @dataclass
(and be renamed to ServiceSpecs
) instead of being a TypedDict
. Maybe I'm missing something, though. (the same applies to the other TypedDict
derivatives below.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified the code as per the comment,
@dataclass
class ServiceSpecs:
diag_layer: str
diag_layer_type: str
new_services: List[str] # List of new service names
deleted_services: List[DiagService] # List of deleted services
renamed_service: List[Tuple[str, DiagService]] # List of (old_name, DiagService)
changed_name_of_service: List[Tuple[str, DiagService]] # Similar to renamed_service
changed_parameters_of_service: List[Tuple[str, DiagService, List[Union[str, int, float]]]]
@dataclass
class SpecsChangesVariants:
new_variants: List[str]
deleted_variants: List[str]
service_changes: ServiceSpecs
odxtools/cli/compare.py
Outdated
"changed_name_of_service"][0] or service_dict["changed_parameters_of_service"][0]: | ||
assert isinstance(service_dict["diag_layer"], str) | ||
if ( | ||
service_dict["new_services"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if SpecsServiceDict
becomes a dataclass, this should change this to service_spec.new_services
(assuming you rename the service_dict
variable to service_spec
). I'm not sure if mypy can handle typed dictionaries properly, but IMO directly accessing the attributes is considerably less clunky...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified as service_dict
variable to service_spec
but the mypy
couldn't handle it... It throws the error.
odxtools/cli/compare.py
Outdated
or service_dict["changed_name_of_service"] | ||
or service_dict["changed_parameters_of_service"] | ||
): | ||
# assert isinstance(service_dict["diag_layer"], str) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove such obsolete code instead of commenting it out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the obsolete code.
odxtools/cli/compare.py
Outdated
list, # type: ignore[type-arg] | ||
service_dict["changed_parameters_of_service"])[2][service_idx] | ||
info_list = cast(list, service_dict["changed_parameters_of_service"])[ | ||
2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that 2
seems too much of a magical number to me. Ideally, this would be something like
service_spec.changed_parameters.added_params
(or whatever the third entry of that list represents)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @andlaus, I debugged the magical number 2
by running the test on my local machine and found that it is a list,
service_spec["changed_parameters_of_service"][2]: [[" Properties of positive response parameter 'status' that have changed:\n", {'Property': ['Bit Length', 'Linked DOP object', ' DOP name'], 'Old Value': [16, '', 'uint16'], 'New Value': [8, '', 'uint8']}], [" Properties of positive response parameter 'num_flips_done' that have changed:\n", {'Property': ['Byte position'], 'Old Value': [2], 'New Value': [None]}]]
In the above case the magical number 2
is mandatory to print the detailed table. Please let me know if there is anything I can change?
Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it's things like this that make the compare tool code so difficult to percolate: in this case the second element of the list passed to print_dl_changes()
represents the detailed information about what changed with the parameters (cf. https://github.com/mercedes-benz/odxtools/blob/main/odxtools/cli/compare.py#L493-L496 ). IMO it would be great if this was immediately obvious by looking at the code, e.g. the above should become service_spec.changed_parameters[param_idx].change_details
with the change_details
attribute being a proper dataclass as well...
Overall executed the
|
…rmat-source.sh etc.
yes. ideally you run this script before you commit anything, so you don't need to worry about the formatting... |
(before you push to github, it is IMO advisable to run |
Ok let me work on it and thanks for letting me know |
Hi @andlaus
Added these set of lines which is used to reduce the
type :ignore
comments incompare.py
file which increases the readability and to maintain a structured dict. Please check these line commits and let me know I can help on this regard.class SpecsServiceDict(TypedDict): diag_layer: str diag_layer_type: str new_services: List[str] # List of new service names deleted_services: List[DiagService] # List of deleted services renamed_service: List[Tuple[str, DiagService]] # List of (old_name, DiagService) changed_name_of_service: List[Tuple[str, DiagService]] # Similar to renamed_service changed_parameters_of_service: List[Tuple[str, DiagService, List[Union[str, int, float]]]] class SpecsChangesVariants(TypedDict): new_variants : List[str] deleted_variants : List[str] service_changes : SpecsServiceDict
Thanks
Vinoth Kannan