ruff icon indicating copy to clipboard operation
ruff copied to clipboard

raw pathlib.Path truthyness checking

Open hairmare opened this issue 1 year ago • 5 comments

As a strong proponent of ALL my colleague and I have recently stumbled over PTH (flake8-use-pathlib)

Specifically in an effort to replace os.path we ended up doing this by accident

from pathlib import Path
if Path("does-not-exists"):
    ...

We forgot to Path(...).exists(), it should have been this:

from pathlib import Path
if Path("does-not-exists").exists():
    ...

Given this happened in the context of an if clause. Is ensuring that noone tries to check a bare Path for truthiness a check that could/should be introduced?

hairmare avatar Apr 19 '24 20:04 hairmare

That seems like a reasonable rule to me, though not sure how best to categorize it.

charliermarsh avatar Apr 19 '24 21:04 charliermarsh

I'm not sure if this rule is too specific. What about other types that are used to check for truthiness?

Should we make the rule more generic so that it warns about all types that don't implement __bool__ and aren't well known types (e.g. list, primitives etc) and are used in a thruthiness check?

MichaReiser avatar Apr 21 '24 11:04 MichaReiser

I'm not sure if this rule is too specific. What about other types that are used to check for truthiness?

Should we make the rule more generic so that it warns about all types that don't implement __bool__ and aren't well known types (e.g. list, primitives etc) and are used in a thruthiness check?

I have another case for this...

import xml.etree.ElementTree

et: xml.etree.ElementTree.Element = xml.etree.ElementTree.fromstring(
    """
<rss version="2.0">
<channel>
<title>title</title>
<link>
<![CDATA[ https://example.com ]]>
</link>
<pubDate>Mon, 22 Apr 2024 16:50:40 +0800</pubDate>
<item>
<title>
<![CDATA[ a title ]]>
</title>
<link>https://exmaple.com/not-expected-url</link>
<enclosure url="https://example.com/expected-url" length="16306828171" type="application/x-bittorrent"/>
<guid isPermaLink="false">guid</guid>
</item>
</channel>
</rss>
"""
)

raw = []

for item in et.findall("channel/item"):
    enclosure = item.find("./enclosure")
    # gotcha !
    # need to use `enclosure is not None`
    if enclosure:
        url = enclosure.attrib.get("url")
    else:
        # torrentleech
        url = item.findtext("./link")

    print(url)

trim21 avatar Apr 22 '24 08:04 trim21

A general __bool__ check does sound like an interesting approach worth further exploring and would help uncovering the error we initially encountered.

The etree example wouldn't be covered by a rule generalized in this way given the Element returned from a find lookup does implement __bool__. In most XML use cases, i usually recommend ensuring the data can be trusted by doing previous validation using XML tooling like XML Schema Definitions.

hairmare avatar Apr 23 '24 19:04 hairmare