basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

Is it possible to scope rules to disable them for specific aspects of the codebase?

Open amitkparekh opened this issue 2 years ago • 4 comments

Is it possible to disable a specific rule for a specific subfolder/package/module? I tried to look at the configuration options for pyright itself and I couldn't see it.

For my current project, I am using Typer/Click to make it easier to have explicit entrypoints. For one of the function args, I have annotated it to be int | None and defaulting to None. Here's an example:

def command(
    *, 
    my_option: Annotated[
        Optional[int], typer.Option(min=0, max=10)
    ] = None,
) -> None: ...

Notably, I used Optional[int] instead of int | None because Typer/Click don't support union types and I'm guessing it's not a priority to support.

Currently, basedpyright flags this as reportDeprecated, which is definitely true. But to do what I need, I don't have a choice.

I don't mind throwing a # noqa at it, but I have a bunch of entrypoint functions where this occurs and adding the ignore every single time is quite cumbersome. I know it's technically wrong, but as it's localised to this very specific situation/aspect of my codebase, I just let it be and got on with it.

I don't want to disable the rule project-wide (I like strict), but that's what I've needed to do. Do you have any thoughts on how I could handle this?

<3 this project.

amitkparekh avatar Apr 17 '24 08:04 amitkparekh

i thought i already raised an issue for this but i can't find it lol.

im thinking we do this by supporting diagnostic severity overrides in executionEnvironments, for example:

executionEnvironments = [
  { root = "src/foo", reportDeprecated = false },
  { root = "src/bar", reportImplicitRelativeImport = false },
]

DetachHead avatar Apr 17 '24 08:04 DetachHead

~~Ah right! I thought that was only for literal environments like Linux vs Mac etc! I didn't realise it did this too!!! I just tried it and it doesn't seem to work? I added the following to my TOML.~~ (Sorry I misread!)

This is totally top of my "basedmpy features" list!

amitkparekh avatar Apr 17 '24 08:04 amitkparekh

idk why pyright doesn't support it when mypy does. it's not even exclusive to basedmypy lol

DetachHead avatar Apr 17 '24 09:04 DetachHead

in the mean time a workaround that should work in your case is using ruff to report deprecated type annotations instead using the pyupgrade rules, since ruff supports per-file ignores

DetachHead avatar Apr 17 '24 10:04 DetachHead

im thinking we do this by supporting diagnostic severity overrides in executionEnvironments, for example:

executionEnvironments = [
  { root = "src/foo", reportDeprecated = false },
  { root = "src/bar", reportImplicitRelativeImport = false },
]

alternatively we could instead support this by fixing the new extends option (#384) and using multiple config files instead. though to quote eric (https://github.com/microsoft/pyright/issues/8019#issuecomment-2137582940):

This would require a significant change internally to pyright

so i'd lean towards whichever approach requires the least amount of changes, to reduce the number of conflicts when i do upstream merges

DetachHead avatar May 29 '24 21:05 DetachHead