Skip to content

Commit

Permalink
Fix a bug in subprocess_overlay.
Browse files Browse the repository at this point in the history
In the presence of an opaque *args or **kwargs, we don't know which overload of
subprocess.Popen.__new__ was matched.

PiperOrigin-RevId: 482882419
  • Loading branch information
rchen152 committed Oct 26, 2022
1 parent e817814 commit 4241cf8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
7 changes: 2 additions & 5 deletions pytype/abstract/_pytd_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,8 @@ def _can_match_multiple(self, args):
for var in args.get_variables():
if any(_isinstance(v, "AMBIGUOUS_OR_EMPTY") for v in var.data):
return True
for arg in (args.starargs, args.starstarargs):
# An opaque *args or **kwargs behaves like an unknown.
if arg and not isinstance(arg, mixin.PythonConstant):
return True
return False
# An opaque *args or **kwargs behaves like an unknown.
return args.has_opaque_starargs_or_starstarargs()

def _match_view(self, node, args, view, alias_map=None):
if self._can_match_multiple(args):
Expand Down
4 changes: 4 additions & 0 deletions pytype/abstract/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,10 @@ def delete_namedarg(self, name):
def replace(self, **kwargs):
return attrs.evolve(self, **kwargs)

def has_opaque_starargs_or_starstarargs(self):
return any(arg and not _isinstance(arg, "PythonConstant")
for arg in (self.starargs, self.starstarargs))


class ReturnValueMixin:
"""Mixin for exceptions that hold a return node and variable."""
Expand Down
5 changes: 4 additions & 1 deletion pytype/overlays/subprocess_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@ def _can_match_multiple(self, args):
if not ambiguous:
return False
found_ambiguous_arg = True
return super()._can_match_multiple(args) if found_ambiguous_arg else False
if found_ambiguous_arg:
return super()._can_match_multiple(args)
else:
return args.has_opaque_starargs_or_starstarargs()
11 changes: 11 additions & 0 deletions pytype/tests/test_stdlib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,17 @@ def run1(value: bool) -> Any: ...
def run2(value: Any) -> Union[bytes, str]: ...
""")

def test_popen_kwargs(self):
self.Check("""
import subprocess
def popen(cmd: str, **kwargs):
kwargs['stdout'] = subprocess.PIPE
kwargs['stderr'] = subprocess.PIPE
process = subprocess.Popen(cmd, **kwargs)
stdout, _ = process.communicate()
assert_type(stdout, 'Union[bytes, str]')
""")

def test_enum(self):
self.Check("""
import enum
Expand Down

0 comments on commit 4241cf8

Please sign in to comment.