codeclimate/codeclimate-duplication

Strange duplication issue

grm opened this issue · 1 comments

grm commented

Hello,

I have two distinct python classes where code climate tells me the code is duplicated.
As i already refactored a lot and they represent two distinct objects, i do not really want to make one object from these two :

from gardena.base_gardena_device_class import BaseGardenaDeviceClass


class WaterControl(BaseGardenaDeviceClass):
    """Class to communicate with a water control device"""

    watering_valve_open = None
    watering_manual_override = None

    """Used to map data between 'watering' ability fields and class fields"""
    watering_outlet_ability_fields = {
        "valve_open": "watering_valve_open",
        "manual_override": "watering_manual_override",
    }

    water_control_ability_type_maps = {
        "watering_outlet": watering_outlet_ability_fields
    }

    def get_device_specific_ability_type_maps(self):
        return self.water_control_ability_type_maps

And

from gardena.base_gardena_device_class import BaseGardenaDeviceClass


class Gateway(BaseGardenaDeviceClass):
    """Class to hold informations about gateways"""

    ip_address = None
    timezone = None

    """Used to map data between 'gateway' ability fields and class fields"""
    gateway_ability_fields = {"ip_address": "ip_address", "time_zone": "timezone"}

    gateway_ability_type_maps = {"gateway": gateway_ability_fields}

    def get_device_specific_ability_type_maps(self):
        return self.gateway_ability_type_maps

Also, i have other classes here and here that follows the same principle but where no duplications are detected.
Is there something I could do about it ?

https://codeclimate.com/github/grm/py-smart-gardena/issues

Thanks a lot for your help,
Jérémie

Those two classes are being identified as "similar" code because they're structurally identical. Exactly the same structure, just different literal values/field names. mower wasn't picked up as also duplication because its structure is slightly different, e.g. its ability fields had 3 keys.

If you don't find similar code identification useful in general, you can turn it off: https://docs.codeclimate.com/docs/disabling-individual-checks. If you're using codeclimate.com and not relying on the CLI, you can also mark those issues as "wontfix": https://docs.codeclimate.com/docs/issues#section-issue-statuses.

In this particular case I think your code is probably ok as-is, but having an inheritance chain where each subclass needs to overwrite that many fields also might be a smell indicating that the composition over inheritance principle might lead to a structure you'd be happier with.