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

Reduced the type: ignore comments and allocated the structured dataclass #393

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

vinothk-master
Copy link

Hi @andlaus

Added these set of lines which is used to reduce the type :ignore comments in compare.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

@CLA-Mercedes-Benz
Copy link

CLA-Mercedes-Benz commented Feb 22, 2025

CLA assistant check
All committers have signed the CLA.


# name of the tool
_odxtools_tool_name_ = "compare"

"""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commented out code

deleted_variants : List[str]
service_changes : SpecsServiceDict
#class extract_service_tabulation_data(TypedDict):
# table: RichTable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commented out code

"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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commented out code

Copy link
Member

@andlaus andlaus left a 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...


SpecsServiceDict = Dict[str, Union[VariantName, VariantType, NewServices, DeletedServices,
RenamedServices, ServicesWithParamChanges]]
class SpecsServiceDict(TypedDict):
Copy link
Member

@andlaus andlaus Feb 24, 2025

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.)

Copy link
Author

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

"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"]
Copy link
Member

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...

Copy link
Author

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.

or service_dict["changed_name_of_service"]
or service_dict["changed_parameters_of_service"]
):
# assert isinstance(service_dict["diag_layer"], str)
Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the obsolete code.

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
Copy link
Member

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)

Copy link
Author

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]}]]

Screenshot 2025-02-24 200556

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

Copy link
Member

@andlaus andlaus Feb 25, 2025

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...

@vinothk-master
Copy link
Author

Member

Overall executed the reformat-source.sh as a result it would satisfy the formatting conditions.

$ sh reformat-source.sh
Fixing E:\OpenSource Contributions\Mercedes\odxtools\odxtools\basicstructure.py
Fixing E:\OpenSource Contributions\Mercedes\odxtools\odxtools\nameditemlist.py
Fixing E:\OpenSource Contributions\Mercedes\odxtools\odxtools\odxtypes.py
Fixing E:\OpenSource Contributions\Mercedes\odxtools\odxtools\request.py
Fixing E:\OpenSource Contributions\Mercedes\odxtools\odxtools\response.py
Fixing E:\OpenSource Contributions\Mercedes\odxtools\odxtools\cli\compare.py
Fixing E:\OpenSource Contributions\Mercedes\odxtools\odxtools\diaglayers\hierarchyelement.py
Fixing E:\OpenSource Contributions\Mercedes\odxtools\tests\test_compu_methods.py

@andlaus
Copy link
Member

andlaus commented Feb 25, 2025

Overall executed the reformat-source.sh as a result it would satisfy the formatting conditions.

yes. ideally you run this script before you commit anything, so you don't need to worry about the formatting...

@andlaus
Copy link
Member

andlaus commented Feb 25, 2025

(before you push to github, it is IMO advisable to run mypy and ruff as well because these are build into the CI and your PR will fail if these two do not pass locally...)

@vinothk-master
Copy link
Author

(before you push to github, it is IMO advisable to run mypy and ruff as well because these are build into the CI and your PR will fail if these two do not pass locally...)

Ok let me work on it and thanks for letting me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants