Skip to content

Commit fcfb294

Browse files
authored
Fix stubs-only resolver incorrectly triggered when inspect.signature available leading to missing parameter defaults (#724)
1 parent 644f11b commit fcfb294

File tree

6 files changed

+16
-11
lines changed

6 files changed

+16
-11
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Fixed
3030
<https://github.com/omni-us/jsonargparse/pull/717>`__).
3131
- ``TypedDict`` values not validated when types are forward references (`#722
3232
<https://github.com/omni-us/jsonargparse/pull/722>`__).
33+
- Stubs-only resolver incorrectly triggered when ``inspect.signature`` available
34+
leading to missing parameter defaults (`#724
35+
<https://github.com/omni-us/jsonargparse/pull/724>`__).
3336

3437
Changed
3538
^^^^^^^

jsonargparse/_parameter_resolvers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,11 @@ def get_parameters_from_stubs(
10421042
logger: logging.Logger,
10431043
) -> Optional[ParamList]:
10441044
component, parent, _ = get_component_and_parent(function_or_class, method_or_property)
1045+
try:
1046+
inspect.signature(component)
1047+
return None
1048+
except Exception:
1049+
pass # only from stubs if getting signature fails
10451050
params = None
10461051
resolver = get_stubs_resolver()
10471052
stub_import = resolver.get_component_imported_info(component, parent)

jsonargparse/_stubs_resolver.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from contextlib import suppress
55
from copy import deepcopy
66
from importlib import import_module
7-
from pathlib import Path
87
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple
98

109
from ._optionals import import_typeshed_client, typeshed_client_support
@@ -105,9 +104,7 @@ def find(self, node: ast.AST, method_name: str) -> Optional[ast.FunctionDef]:
105104
def get_stubs_resolver():
106105
global stubs_resolver
107106
if not stubs_resolver:
108-
search_path = [Path(p) for p in sys.path]
109-
search_context = tc.get_search_context(search_path=search_path)
110-
stubs_resolver = StubsResolver(search_context=search_context)
107+
stubs_resolver = StubsResolver()
111108
return stubs_resolver
112109

113110

@@ -134,8 +131,8 @@ def get_source_module(path: str, component) -> tc.ModulePath:
134131

135132

136133
class StubsResolver(tc.Resolver):
137-
def __init__(self, search_context=None) -> None:
138-
super().__init__(search_context)
134+
def __init__(self, **kwargs) -> None:
135+
super().__init__(**kwargs)
139136
self._module_ast_cache: Dict[str, Optional[ast.AST]] = {}
140137
self._module_assigns_cache: Dict[str, Dict[str, ast.Assign]] = {}
141138
self._module_imports_cache: Dict[str, Dict[str, Tuple[Optional[str], str]]] = {}
@@ -144,8 +141,8 @@ def get_imported_info(self, path: str, component=None) -> Optional[tc.ImportedIn
144141
resolved = self.get_fully_qualified_name(path)
145142
imported_info = None
146143
if isinstance(resolved, tc.ImportedInfo):
147-
resolved = resolved.info
148-
if isinstance(resolved, tc.NameInfo):
144+
imported_info = resolved
145+
elif isinstance(resolved, tc.NameInfo):
149146
source_module = get_source_module(path, component)
150147
imported_info = tc.ImportedInfo(source_module=source_module, info=resolved)
151148
return imported_info

jsonargparse_tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def capture_logs(logger: logging.Logger) -> Iterator[StringIO]:
185185

186186
@contextmanager
187187
def source_unavailable():
188-
with patch("inspect.getsource", side_effect=OSError("could not get source code")):
188+
with patch("inspect.getsource", side_effect=OSError("mock source code not available")):
189189
yield
190190

191191

jsonargparse_tests/test_postponed_annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_get_types_pep604():
7373
def test_get_types_pep604_source_unavailable(logger):
7474
with source_unavailable(), pytest.raises(TypeError) as ctx, capture_logs(logger) as logs:
7575
get_types(function_pep604, logger)
76-
ctx.match("could not get source code")
76+
ctx.match("mock source code not available")
7777
assert "Failed to parse to source code" in logs.getvalue()
7878

7979

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ all = [
5252
signatures = [
5353
"jsonargparse[typing-extensions]",
5454
"docstring-parser>=0.15",
55-
"typeshed-client>=2.1.0",
55+
"typeshed-client>=2.3.0",
5656
]
5757
jsonschema = [
5858
"jsonschema>=3.2.0",

0 commit comments

Comments
 (0)