4

I'm using Snakemake and it's been rather enjoyable, but I would love it if VSCode stopped complaining about Undefined variable: 'snakemake'. The issue is that Snakemake runs jobs in such a way that it injects this global into your python scope, but IDEs have no idea that it really is defined. I am wondering if there is a way to get it to do two things:

  1. Stop complaining, at the least
  2. Do autocomplete--it should autocomplete based on the rules defined in the Snakefile (input, output, params, etc.) and its own APIs.

Undefined variable: 'snakemake'

The VSCode extension for Snakemake only relates to the Snakefile syntax. Elsewhere VSCode uses pylint.

Any ideas?

wulftone
  • 1,628
  • 1
  • 18
  • 34
  • Have you tried declaring `global snakemake` to tell Python it's supposed to pull from the global scope? I'd be wary of any tool that uses globals like this in Python, though; "explicit is better than implicit" is a pretty good general rule. Also, please post code as text in a code block rather than as an image. – kungphu Jun 10 '20 at 23:50
  • 1
    Unfortunately, declaring it as a global doesn't do anything since it's still undefined, unless I'm misunderstanding your idea. As for the image: I didn't think this code was important, it was more important to see how VSCode shows it is an error, but I can see how it might be useful. – wulftone Jun 12 '20 at 16:34

3 Answers3

2

Only slightly better than the other answer is to do:

smk = snakemake # type: ignore
mlp = load_python_obj(smk.input.mlp_model_filename)

This inlines the exclusion to that one line (where snakemake is undefined), but allows the definition of smk so that when when you refer to smk there is no complaint. This allows other relevant cases of undefined variables to still be identified rather than excluding all of them.

alphabetasoup
  • 537
  • 7
  • 15
1

This is a terrible solution, but thus far the only one that I have managed to find. I hope that this non-solution can be quickly supplanted by a better one.

At the top of the script file you can add

# pyright: reportUndefinedVariable=false

This removes the warning (and all other undefined variable warnings). If you have flake8 as well, you will have to use # noqa: F821 on each line as well.

gremble
  • 11
  • 5
  • Is there a way to have both `type: ignore` for pylint and `noqa: F821` for flake8 at the same time. Superlinter makes both complaints and I can't seem to quiet both. It's either one or the other... Ugh. – hepcat72 Jun 15 '23 at 22:09
0

This won't remove the error, but it will disable all error messages. Hit Ctrl-Shift-P and type disable error messages in the top. Click that and they will completely disappear (it won't fix the errors though)