Skip to content

Usage of check_file to display which pattern failed #108

@henryiii

Description

@henryiii

I'd like to add the failing pattern to my debug logging. I tried this:

    if (c := include_spec.check_file(p)).include:
        logger.debug(
            "Including {} {} because it is explicitly included by rule {!r}.",
            ptype,
            p,
            include_spec.patterns[c.index].pattern,
        )
        return True

But I've run into two or three typing issues.

  1. Pattern has no attribute .pattern; this could be solved if the include spec was made Generic so that it could know it has a subclass, but is there a better way to print out an arbitrary Pattern? __str__() is not overloaded.
  2. c.index could be None, so I get a complaint indexing patterns with it, except it can't be None if .include is True, I believe, so there might be a way to use a type guard or something here to indicate that these two types are linked.
  3. .patterns is a Sequence (instead of a Collection from Python 3.6+), so you can't index it. It would be much more expensive for me to do list(include_spec.patterns)[c.index], since I know it's a list, but maybe this should be typed differently? Is there a reason to only allow Sequence here, vs. allowing random access?

Those issues make me think maybe I'm not approaching this correctly; upon finding check_file as a replacement for match_file, I was pretty sure that's what this was for, but maybe there's a better way?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions