Menu

#442 Is `typ` a str or list of strings?

closed
nobody
None
minor
bug
2023-05-07
2022-11-01
Nick Brown
No

The following code based on the example:

yaml = YAML(typ=["rt", "string"])

results in the following mypy error:

Argument "typ" to "YAML" has incompatible type "List[str]"; expected "Optional[str]"

Looking at https://sourceforge.net/u/jspricke/ruamel-yaml/ci/default/tree/main.py#l55
It appears to be typed hinted as Optional[Text]

However at https://sourceforge.net/u/jspricke/ruamel-yaml/ci/default/tree/main.py#l66

self.typ = ['rt'] if typ is None else (typ if isinstance(typ, list) else [typ])

It's always initialised as a list of typ.

In most the code that operates on typ it uses in which can work on either a list or a string.

So assuming that support for either a single string containing the different types, or list of strings of the types is intended, then perhaps the type hint should be Optional[List[Text] | Text] ?

Or should the type hint just be Optional[List[Text]]?

Discussion

  • Anthon van der Neut

    It should be Optional[List[Text] | Text].
    Although internally a list of strings is presumed, for convenience you can provide just a string argument that is converted to a list of strings.

     
  • Anthon van der Neut

    Fixed in 0.17.23, I had to change typ to Optional[Union[List[Text], Text]] as the
    Union Operator from PEP 604 was not introduced until Python 3.10.

     
  • Anthon van der Neut

    • status: open --> closed
     

Log in to post a comment.

MongoDB Logo MongoDB