Closed GlebMorgan closed 2 weeks ago
Yes, you're correct that typeCheckingMode
isn't supported for execution environments. That's very much by design. I don't think it would make sense to add this because typeCheckingMode
implies a set of default severities for each of the type checking rules, but execution environments represent overrides to severities.
As you've noted, if you want to apply strict mode to a subset of directories or files, there is a strict
setting.
OK, got it regarding typeCheckingMode
overrides, thank you for an explanation 🙂
But then why strict
option does not allow for any rule overrides on top of itself? As far as I experimented, once some files fall under strict
scope, there is no way to adjust the rules applied to these files. The rule set for them is cemented to whatever strict
mode uses by default.
But then why strict option does not allow for any rule overrides on top of itself?
Here's the override order:
typeCheckingMode
setting in the main part of the config file establishes the defaults rule setreportUnnecessaryCompare
) overrides 1strict
setting overrides previous and applies the strict rule set# pyright
comments overrides previous# pyright: ignore
comments overrides previousignore
setting overrides previousSo, if you want to apply rules on top of the strict
option, you can do so using file-specific # pyright
comment.
The strict
setting has a high precedence is based on my own experience managing large teams with large code bases. I want to ensure that certain directories (especially newly-written code) used strict-mode settings. I didn't want developers on my team to intentionally or unintentionally disable some of the strict-mode checks for those files in the config file. If a strict-mode check must be disabled for some reason within a specific file, we use a # pyright
comment in that file and accompany it with a lengthy comment justifying why it's OK to disable that check for that file.
I very much appreciate your detailed answers 🙂
In my personal opinion, the whole purpose of more specific settings is to override more generic ones, otherwise generic options might not end up being useful at all, like in my example.
Ideal option IMHO would be to let each execution environment have its own ruleset (maybe even defined in a separate config seciton) and then deal with any exceptions via # pyright
comments individually on per-file or per-line basis. But as of now, at least in usecases similar to mine the only way to find strict
setting useful is to fully conform to the rule set it provides, since it is rigidly determined by Pylint. I would say that is not always favorable.
But I respect your experience, probably just looking at the functionality Pylance provides from a different perspective.
I'm using Pylance in VSCode. I want to specify different type checking settings / rules (at least type checking mode) for the application and tests separately.
Consider a simple project structure with
app/
andtests/
directories:Say, I want type checking in test files to be more forgiving (since they are just tests). So, I decided to set
typeCheckingMode
tostrict
for files insideapp/
and tostandard
– insidetests/
. I suppose the intended way to do it is using execution environments (even though this reply states this is not possible at all).Docs for "Execution Environment Options" say:
The corresponding link leads to the section where
typeCheckingMode
option is also mentioned.Great, let's try this:
Apparently, that does not work. Pylance continues to use the default value for
typeCheckingMode
option (standard
) and ignores execution environment overrides.Does it mean
typeCheckingMode
option is not overridable by execution environment settings and only rule overrides are supposed to work? Is that intended? Because for me these seem to always work in conjunction:typeCheckingMode
specifies a general "base" profile (rule set) to start with and then specific rules can be further adjusted to be more or less strict.OK, there seems to be a different approach – a
strict
option:This kinda works at first glance, but in this case all rule overrides stop working for
app/
directory. Neither globalreport...
settings nor their execution environment counterparts have any effect on the files insideapp/
. Besides, iftypeCheckingMode
override would've worked for execution environments,strict
option would seem kinda redundant in general...There are some other alternatives:
strict
option, settypeCheckingMode
option tostrict
globally and then specify a bunch of rule overrides fortests/
to make it essentially "basic" – this will quickly make the setting file a mess and kinda defeat the whole purpose oftypeCheckingMode
option as a baseline rule set.# pyright: standard
in every test file as suggested here – better, but this approach kinda splits configuration betweenpyrightconfig.json
and every test file (and also rule exceptions and type checking modes will be set in different places).tests/
directory and link it withextends
option – as extreme as it is, I still tried this approach but never got it to work. Besides, I believeextends
is not even supposed to override stuff only for a subdirectory it is located in.tests/
completely – that will work as a complete fallback option, but I would still like Pylance to give me some insights for test files, just without going too pedantic.So, my question is: what is the intended way to have different Pylance/Pyright settings for different parts of the project? And why
executionEnvironments
option does not supporttypeCheckingMode
override despite (as far as I understood) it should according to docs?