admin管理员组

文章数量:1310099

Below is my python code where I am trying to pass kubeseal_cmd and run_options to subprocess.run method, but it's giving me error

No overload variant of subprocess.run matches argument types list[str], dict[str,object].

What am I missing? I am using python 3.12

kubeseal_path = "/var/tmp/workspace/file.txt"
secret = yaml.safe_load(secret_File.read().encode("utf-8"))
cert_file = = "/var/tmp/workspace/file123.txt"
kubeseal_cmd = [
   kubeseal_path,
   "--cert",
   cert_file,
   "--format=yaml",
   "</dev/stdin>"
]
run_options = {
   "env": {},
   "stdout": subprocess.PIPE,
   "check": True,
   "input": yaml.dump(secret).encode(),
}
sealed_secret = yaml.safe_load(subprocess.run(kubeseal_cmd, **run_options).stdout.decode().strip())

Below is my python code where I am trying to pass kubeseal_cmd and run_options to subprocess.run method, but it's giving me error

No overload variant of subprocess.run matches argument types list[str], dict[str,object].

What am I missing? I am using python 3.12

kubeseal_path = "/var/tmp/workspace/file.txt"
secret = yaml.safe_load(secret_File.read().encode("utf-8"))
cert_file = = "/var/tmp/workspace/file123.txt"
kubeseal_cmd = [
   kubeseal_path,
   "--cert",
   cert_file,
   "--format=yaml",
   "</dev/stdin>"
]
run_options = {
   "env": {},
   "stdout": subprocess.PIPE,
   "check": True,
   "input": yaml.dump(secret).encode(),
}
sealed_secret = yaml.safe_load(subprocess.run(kubeseal_cmd, **run_options).stdout.decode().strip())
Share Improve this question edited Jan 15 at 19:27 InSync 10.4k4 gold badges15 silver badges51 bronze badges asked Jan 13 at 19:51 BokamboBokambo 4,48028 gold badges87 silver badges146 bronze badges 11
  • Please make a minimal reproducible example. YAML doesn't seem to be relevant to the problem, and some of these variables are undefined. – wjandrea Commented Jan 13 at 19:58
  • 1 That's not a real error, it's bad type hints being used for static checking (specifically, it's what I'd expect a static checking tool to do if you weren't using **). A real error would have an exception and stack trace. Anyhow -- why are you using a run_options dict at all? If you inline the keyword arguments (ditch run_options entirely and instead pass check=True &c), I'd expect that to moot the immediate issue. – Charles Duffy Commented Jan 13 at 21:32
  • 1 @Bokambo, ...to be clear, there's magic you can do using a TypedDict definition that matches the subprocess.run kwargs to make the typechecker work right under these circumstances, but I don't see any reason it would be worth the bother. – Charles Duffy Commented Jan 13 at 22:35
  • 1 @CharlesDuffy : Don't you mind posting complete answer and i can accept it. – Bokambo Commented Jan 14 at 0:48
  • 2 Please add a tag for the type checker that's producing this warning -- mypy, pylint, etc. – Barmar Commented Jan 14 at 1:36
 |  Show 6 more comments

1 Answer 1

Reset to default 1

The Easy Answer

"Hey, doctor, it hurts when I do this!"
"Don't do that"

Nothing your code is doing requires the run_options dict. Take it out and the problem is mooted:

sealed_secret = yaml.safe_load(
    subprocess.run(
        kubeseal_cmd,
        env={},
        stdout=subprocess.PIPE,
        check=True,
        input=yaml.dump(secret).encode(),
    ).stdout.decode().strip()
)

The Other Approach

If you really want to be able to represent keyword arguments in a dict, you can do that by building a TypedDict setting out the type of each item.

Note that this requires a quite current Python release -- the below code is tested on Python 3.12.

from typing import Any, Literal, TypedDict, NotRequired, IO

class RunOptions(TypedDict):
   env: NotRequired[dict[str, str]]
   check: NotRequired[bool]
   input: NotRequired[None|str|bytes]
   stdout: NotRequired[int | IO[Any]]

run_options = RunOptions({
   "env": {},
   "stdout": subprocess.PIPE,
   "check": True,
   "input": yaml.dump(secret).encode(),
})
sealed_secret = yaml.safe_load(
    subprocess.run(kubeseal_cmd, **run_options).stdout.decode().strip()
)

本文标签: