@@ -93,26 +93,40 @@ def iterparentnodeids(nodeid: str) -> Iterator[str]:
93
93
yield nodeid
94
94
95
95
96
+ def _check_path (path : Path , fspath : LEGACY_PATH ) -> None :
97
+ if Path (fspath ) != path :
98
+ raise ValueError (
99
+ f"Path({ fspath !r} ) != { path !r} \n "
100
+ "if both path and fspath are given they need to be equal"
101
+ )
102
+
103
+
96
104
def _imply_path (
97
105
path : Optional [Path ], fspath : Optional [LEGACY_PATH ]
98
106
) -> Tuple [Path , LEGACY_PATH ]:
99
107
if path is not None :
100
108
if fspath is not None :
101
- if Path (fspath ) != path :
102
- raise ValueError (
103
- f"Path({ fspath !r} ) != { path !r} \n "
104
- "if both path and fspath are given they need to be equal"
105
- )
106
- assert Path (fspath ) == path , f"{ fspath } != { path } "
109
+ _check_path (path , fspath )
107
110
else :
108
111
fspath = legacy_path (path )
109
112
return path , fspath
110
-
111
113
else :
112
114
assert fspath is not None
113
115
return Path (fspath ), fspath
114
116
115
117
118
+ # Optimization: use _imply_path_only over _imply_path when only need Path.
119
+ # This is to avoid `legacy_path(path)` which is surprisingly heavy.
120
+ def _imply_path_only (path : Optional [Path ], fspath : Optional [LEGACY_PATH ]) -> Path :
121
+ if path is not None :
122
+ if fspath is not None :
123
+ _check_path (path , fspath )
124
+ return path
125
+ else :
126
+ assert fspath is not None
127
+ return Path (fspath )
128
+
129
+
116
130
_NodeType = TypeVar ("_NodeType" , bound = "Node" )
117
131
118
132
@@ -196,7 +210,9 @@ def __init__(
196
210
self .session = parent .session
197
211
198
212
#: Filesystem path where this node was collected from (can be None).
199
- self .path = _imply_path (path or getattr (parent , "path" , None ), fspath = fspath )[0 ]
213
+ self .path = _imply_path_only (
214
+ path or getattr (parent , "path" , None ), fspath = fspath
215
+ )
200
216
201
217
# The explicit annotation is to avoid publicly exposing NodeKeywords.
202
218
#: Keywords/markers collected from all scopes.
@@ -573,7 +589,7 @@ def __init__(
573
589
assert path is None
574
590
path = path_or_parent
575
591
576
- path , fspath = _imply_path (path , fspath = fspath )
592
+ path = _imply_path_only (path , fspath = fspath )
577
593
if name is None :
578
594
name = path .name
579
595
if parent is not None and parent .path != path :
0 commit comments