You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the function updateExtendPolishDictionaryLists, for the elements of the list, is it possible to use a type hint more specific than Any?
I would like to ask a better question, but I am struggling to discriminate the important concepts from everything else going on in the function.
I have essentially no experience with Protocol, but I've been using this prototype for a short while. On the other hand, I have literally only used it with non-negative int--that are less than 64.
fromcytoolz.functoolzimportcurryassyntacticCurryfromtypingimportProtocolclassOrdinals(Protocol):
"""Protocol for types that support ordering comparisons."""def__le__(self, other: "Ordinals", /) ->bool:
"""Less than or equal to comparison."""
...
def__ge__(self, other: "Ordinals", /) ->bool:
"""Greater than or equal to comparison."""
...
@syntacticCurrydefbetween[小于: Ordinals](floor: 小于, ceiling: 小于, comparand: 小于) ->bool:
"""Inclusive `floor <= comparand <= ceiling`."""returnfloor<=comparand<=ceiling
If possible, I would like to improve the type hints in updateExtendPolishDictionaryLists, but I can't find the right combination. If I change ePluribusUnum from dict[..., list[...]] to Sequence[...], then .extend complains about not being a list, and I don't know how to fix that, even though I suspect it is easy.
I've tried different combinations of Mapping, Sequence, and using the Union operator. This answer is thorough and clear, and I almost understand it, but I can't figure out if it is or isn't relevant to my goal.
fromtypingimportLiteral, overload, Protocol, TYPE_CHECKING, TypeVarifTYPE_CHECKING:
fromcollections.abcimportSequenceclassOrdinals(Protocol):
"""Protocol for types that support ordering comparisons."""def__le__(self, other: "Ordinals", /) ->bool:
"""Less than or equal to comparison."""
...
def__ge__(self, other: "Ordinals", /) ->bool:
"""Greater than or equal to comparison."""
...
个=TypeVar('个')
小于=TypeVar('小于', bound=Ordinals)
@overloaddefupdateExtendPolishDictionaryLists(
*dictionaryLists: Mapping[str, list[小于] |set[小于] |tuple[小于, ...]]
, destroyDuplicates: bool=False
, reorderLists: Literal[True] =True
, killErroneousDataTypes: bool=False
) ->dict[str, list[小于]]: ...
@overloaddefupdateExtendPolishDictionaryLists(
*dictionaryLists: Mapping[str, list[个] |set[个] |tuple[个, ...]]
, destroyDuplicates: bool=False
, reorderLists: Literal[False] =False
, killErroneousDataTypes: bool=False
) ->dict[str, list[个]]: ...
defupdateExtendPolishDictionaryLists(
*dictionaryLists: Mapping[str, list[个] |set[个] |tuple[个, ...]] |Mapping[str, list[小于] |set[小于] |tuple[小于, ...]]
, destroyDuplicates: bool=False
, reorderLists: bool=False
, killErroneousDataTypes: bool=False
) ->dict[str, list[个]] |dict[str, list[小于]]:
"""Merge multiple dictionaries with `list` values into a single dictionary with the `list` values merged. Plus options to destroy duplicates, sort `list` values, and handle erroneous data types. Parameters ---------- *dictionaryLists : Mapping[str, list[Any] | set[Any] | tuple[Any, ...]] Variable number of dictionaries to be merged. If only one dictionary is passed, it will be "polished". destroyDuplicates : bool = False If `True`, removes duplicate elements from the `list`. Defaults to `False`. reorderLists : bool = False If `True`, sorts each `list` value. Defaults to `False`. The elements must be comparable; otherwise, a `TypeError` will be raised. killErroneousDataTypes : bool = False If `True`, suppresses any `TypeError` `Exception` and omits the dictionary key or value that caused the `Exception`. Defaults to `False`. Returns ------- ePluribusUnum : dict[str, list[Any]] A single dictionary with merged and optionally "polished" `list` values. Notes ----- The returned value, `ePluribusUnum`, is a so-called primitive dictionary (`dict`). Furthermore, every dictionary key is a so-called primitive string (*cf.* `str()`) and every dictionary value is a so-called primitive `list` (`list`). If `dictionaryLists` has other data types, the data types will not be preserved. That could have unexpected consequences. Conversion from the original data type to a `list`, for example, may not preserve the order even if you want the order to be preserved. """ePluribusUnum: dict[str, list[个]] |dict[str, list[小于]] = {}
fordictionaryListTargetindictionaryLists:
forkeyName, keyValueindictionaryListTarget.items():
try:
ImaStr=str(keyName)
ImaList: Sequence[个|小于] =list(keyValue)
ePluribusUnum.setdefault(ImaStr, []).extend(ImaList)
exceptTypeError:
ifkillErroneousDataTypes:
continueelse:
raiseifdestroyDuplicates:
forImaStr, ImaListinePluribusUnum.items():
ePluribusUnum[ImaStr] =list(dict.fromkeys(ImaList))
ifreorderLists:
forImaStr, ImaRichComparisonSupporterinePluribusUnum.items():
ePluribusUnum[ImaStr] =sorted(ImaRichComparisonSupporter)
returnePluribusUnum
The two diagnostic messages
Argumentof type "list[个@updateExtendPolishDictionaryLists | 小于@updateExtendPolishDictionaryLists]"cannotbeassignedtoparameter"iterable"of type "Iterable[个@updateExtendPolishDictionaryLists]"infunction"extend""list[个@updateExtendPolishDictionaryLists | 小于@updateExtendPolishDictionaryLists]"isnotassignableto"Iterable[个@updateExtendPolishDictionaryLists]"Typeparameter"_T_co@Iterable"iscovariant, but"个@updateExtendPolishDictionaryLists | 小于@updateExtendPolishDictionaryLists"isnotasubtypeof"个@updateExtendPolishDictionaryLists"Type"个@updateExtendPolishDictionaryLists | 小于@updateExtendPolishDictionaryLists"isnotassignableto type "个@updateExtendPolishDictionaryLists"Argumentoftype"list[个@updateExtendPolishDictionaryLists]"cannotbeassignedtoparameter"iterable"oftype"Iterable[SupportsRichComparisonT@sorted]"infunction"sorted""list[个@updateExtendPolishDictionaryLists]"isnotassignableto"Iterable[SupportsRichComparisonT@sorted]"Typeparameter"_T_co@Iterable"iscovariant, but"个@updateExtendPolishDictionaryLists"isnotasubtypeof"SupportsRichComparisonT@sorted"Type"个@updateExtendPolishDictionaryLists"isnotassignableto type "SupportsRichComparison"Type"个@updateExtendPolishDictionaryLists"isnotassignableto type "SupportsRichComparison""object*"isincompatiblewithprotocol"SupportsDunderLT[Any]""object*"isincompatiblewithprotocol"SupportsDunderGT[Any]"
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
In the function
updateExtendPolishDictionaryLists, for the elements of thelist, is it possible to use a type hint more specific thanAny?I would like to ask a better question, but I am struggling to discriminate the important concepts from everything else going on in the function.
I have essentially no experience with
Protocol, but I've been using this prototype for a short while. On the other hand, I have literally only used it with non-negativeint--that are less than 64.(This package requires <= Python 3.12:)
If possible, I would like to improve the type hints in
updateExtendPolishDictionaryLists, but I can't find the right combination. If I changeePluribusUnumfromdict[..., list[...]]toSequence[...], then.extendcomplains about not being alist, and I don't know how to fix that, even though I suspect it is easy.I've tried different combinations of Mapping, Sequence, and using the Union operator. This answer is thorough and clear, and I almost understand it, but I can't figure out if it is or isn't relevant to my goal.
Similarly, I think I understand at least half of this discussion about
SupportsRichComparison, and maybe it means I can't be more specific thanAny, but I'm not sure.I tried this silliness:
It doesn't work, of course, because 'Declaration "ePluribusUnum" is obscured by a declaration of the same name.'
This issue is not critical, which is why I hope it is a good way for me to learn more: less pressure.
This package requires <= Python 3.11:
The two diagnostic messages
Beta Was this translation helpful? Give feedback.
All reactions