Skip to content

multimethod does not recognize correctly 'collections.abc.Container' types #82

@Dvergatal

Description

@Dvergatal

As in subject I'm having an issue that Container objects are not correctly verified just like in below example:

from typing import Any, List, Container
from multimethod import multimethod


@multimethod
def validate_enum_list(key: str, value: Any, allowed_values: Any):
    return NotImplemented

@validate_enum_list.register
def _(key: str, value: str, allowed_values: Container[str]) -> None:
    print("key: str, value: str, allowed_values: Container[str]")
    print(type(value))
    print(type(allowed_values))

@validate_enum_list.register
def _(key: str, value: Container[str], allowed_values: Container[Container[str]]) -> None:
    print("key: str, value: Container[str], allowed_values: Container[Container[str]]")
    print(type(value))
    print(type(allowed_values))

validate_enum_list("test1", "test1", ["test1"])
validate_enum_list("test2", ["test2"], [["test2"]])
validate_enum_list("test2", "test2", "test2")

According to this the third call of validate_enum_list should not be printed as it returns NotImplemented but instead the otuput looks like:

key: str, value: str, allowed_values: Container[str]
<class 'str'>
<class 'list'>
key: str, value: Container[str], allowed_values: Container[Container[str]]
<class 'list'>
<class 'list'>
key: str, value: str, allowed_values: Container[str]
<class 'str'>
<class 'str'>

I have verified that if I will add additional method to serve such case than it's all OK but I wanted to have it served with the NotImplemented pattern. Any better solution how to handle it or maybe there is a bug in multimethod code?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions