| Total Complexity | 10 |
| Total Lines | 37 |
| Duplicated Lines | 40.54 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | import typing |
||
| 2 | |||
| 3 | |||
| 4 | def common_ancestor(*args: object) -> type: |
||
| 5 | """ |
||
| 6 | Get the closest common ancestor of the given objects. |
||
| 7 | :param args: any objects. |
||
| 8 | :return: the ``type`` of the closest common ancestor of the given ``args``. |
||
| 9 | """ |
||
| 10 | return _common_ancestor(args, False) |
||
| 11 | |||
| 12 | |||
| 13 | def common_ancestor_of_types(*args: type) -> type: |
||
| 14 | """ |
||
| 15 | Get the closest common ancestor of the given classes. |
||
| 16 | :param args: any classes. |
||
| 17 | :return: the ``type`` of the closest common ancestor of the given ``args``. |
||
| 18 | """ |
||
| 19 | return _common_ancestor(args, True) |
||
| 20 | |||
| 21 | |||
| 22 | View Code Duplication | def _common_ancestor(args: typing.Sequence[object], types: bool) -> type: |
|
|
|
|||
| 23 | from typish.functions._get_type import get_type |
||
| 24 | from typish.functions._get_mro import get_mro |
||
| 25 | |||
| 26 | if len(args) < 1: |
||
| 27 | raise TypeError('common_ancestor() requires at least 1 argument') |
||
| 28 | tmap = (lambda x: x) if types else get_type |
||
| 29 | mros = [get_mro(tmap(elem)) for elem in args] |
||
| 30 | for cls in mros[0]: |
||
| 31 | for mro in mros: |
||
| 32 | if cls not in mro: |
||
| 33 | break |
||
| 34 | else: |
||
| 35 | # cls is in every mro; a common ancestor is found! |
||
| 36 | return cls |
||
| 37 |