-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Expand file tree
/
Copy pathtest_validate.py
More file actions
475 lines (369 loc) · 14.8 KB
/
Copy pathtest_validate.py
File metadata and controls
475 lines (369 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import ast
from textwrap import dedent
import pytest
from lfx.custom.validate import (
_get_module_fallbacks,
_resolve_attribute,
create_class,
create_function,
execute_function,
prepare_global_scope,
)
def test_importing_langflow_module_in_lfx():
code = dedent("""from langflow.custom import Component
class TestComponent(Component):
def some_method(self):
pass
""")
result = create_class(code, "TestComponent")
assert result.__name__ == "TestComponent"
def test_importing_langflow_logging_in_lfx():
"""Test that langflow.logging can be imported in lfx context without errors."""
code = dedent("""
from langflow.logging import logger, configure
from langflow.custom import Component
class TestLoggingComponent(Component):
def some_method(self):
# Test that both logger and configure work
configure(log_level="INFO")
logger.info("Test message from component")
return "success"
""")
result = create_class(code, "TestLoggingComponent")
assert result.__name__ == "TestLoggingComponent"
def test_execute_function_supports_aliased_dotted_imports():
code = dedent("""
import urllib.request as request
def to_url(path):
return request.pathname2url(path)
""")
assert execute_function(code, "to_url", "folder name/file.txt") == "folder%20name/file.txt"
def test_execute_function_supports_non_aliased_dotted_imports():
"""Regression test: `import urllib.request` then using `urllib.request.X` in execute_function."""
code = dedent("""
import urllib.request
def to_url(path):
return urllib.request.pathname2url(path)
""")
assert execute_function(code, "to_url", "folder name/file.txt") == "folder%20name/file.txt"
def test_execute_function_supports_deep_dotted_imports():
"""Ensure 3+ level dotted imports work (e.g., import xml.etree.ElementTree)."""
code = dedent("""
import xml.etree.ElementTree
def make_root(tag):
return xml.etree.ElementTree.Element(tag).tag
""")
assert execute_function(code, "make_root", "root") == "root"
def test_create_function_supports_dotted_imports():
code = dedent("""
import urllib.request
def to_url(path):
return urllib.request.pathname2url(path)
""")
func = create_function(code, "to_url")
assert func("folder name/file.txt") == "folder%20name/file.txt"
def test_prepare_global_scope_keeps_top_level_package_for_dotted_imports():
module = ast.parse(
dedent("""
import urllib.request
def to_url(path):
return urllib.request.pathname2url(path)
""")
)
scope = prepare_global_scope(module)
assert "urllib" in scope
assert scope["urllib"].request.pathname2url("folder name/file.txt") == "folder%20name/file.txt"
def test_prepare_global_scope_supports_aliased_from_imports():
"""Regression test: `from X import Y as Z` must bind Z in scope, not Y."""
module = ast.parse(
dedent("""
from urllib.request import pathname2url as to_url_path
def to_url(path):
return to_url_path(path)
""")
)
scope = prepare_global_scope(module)
assert "to_url_path" in scope
assert "pathname2url" not in scope
assert scope["to_url_path"]("folder name/file.txt") == "folder%20name/file.txt"
def test_create_class_supports_aliased_from_imports():
"""End-to-end: a component using `from X import Y as Z` should load and Z is usable."""
code = dedent("""
from urllib.request import pathname2url as to_url_path
from lfx.custom import Component
class AliasedImportComponent(Component):
def to_url(self, path):
return to_url_path(path)
""")
cls = create_class(code, "AliasedImportComponent")
assert cls.__name__ == "AliasedImportComponent"
assert cls().to_url("folder name/file.txt") == "folder%20name/file.txt"
# ---------------------------------------------------------------------------
# _get_module_fallbacks
# ---------------------------------------------------------------------------
class TestGetModuleFallbacks:
def test_no_fallback_for_unrelated_module(self):
assert _get_module_fallbacks("requests") == ["requests"]
def test_langflow_falls_back_to_lfx(self):
result = _get_module_fallbacks("langflow.custom")
assert result == ["langflow.custom", "lfx.custom"]
def test_langflow_deep_path(self):
result = _get_module_fallbacks("langflow.custom.validate")
assert result == ["langflow.custom.validate", "lfx.custom.validate"]
def test_langchain_falls_back_to_langchain_classic(self):
result = _get_module_fallbacks("langchain.memory")
assert result == ["langchain.memory", "langchain_classic.memory"]
def test_langchain_deep_path(self):
result = _get_module_fallbacks("langchain.chains.base")
assert result == ["langchain.chains.base", "langchain_classic.chains.base"]
def test_langchain_community_not_remapped(self):
assert _get_module_fallbacks("langchain_community.tools") == ["langchain_community.tools"]
def test_langchain_core_not_remapped(self):
assert _get_module_fallbacks("langchain_core.messages") == ["langchain_core.messages"]
def test_bare_langchain_no_fallback(self):
assert _get_module_fallbacks("langchain") == ["langchain"]
def test_bare_langflow_no_fallback(self):
assert _get_module_fallbacks("langflow") == ["langflow"]
def test_only_first_occurrence_replaced(self):
result = _get_module_fallbacks("langchain.langchain.nested")
assert result == ["langchain.langchain.nested", "langchain_classic.langchain.nested"]
def test_original_always_first(self):
"""The original module is always tried first."""
for name in ["langflow.custom", "langchain.agents", "requests"]:
assert _get_module_fallbacks(name)[0] == name
# ---------------------------------------------------------------------------
# _resolve_attribute
# ---------------------------------------------------------------------------
class TestResolveAttribute:
# -- attributes that exist in langchain 1.0 (no fallback needed) --
def test_resolves_existing_attribute(self):
import langchain.agents as mod
result = _resolve_attribute(mod, "langchain.agents", "create_react_agent")
assert result is not None
def test_resolves_existing_tool_attribute(self):
import langchain.tools as mod
result = _resolve_attribute(mod, "langchain.tools", "tool")
assert callable(result)
# -- attributes removed in langchain 1.0 (attribute-level fallback) --
def test_falls_back_for_agent_executor(self):
import langchain.agents as mod
from langchain_classic.agents import AgentExecutor
result = _resolve_attribute(mod, "langchain.agents", "AgentExecutor")
assert result is AgentExecutor
def test_falls_back_for_base_single_action_agent(self):
import langchain.agents as mod
from langchain_classic.agents import BaseSingleActionAgent
result = _resolve_attribute(mod, "langchain.agents", "BaseSingleActionAgent")
assert result is BaseSingleActionAgent
def test_falls_back_for_structured_tool(self):
import langchain.tools as mod
from langchain_classic.tools import StructuredTool
result = _resolve_attribute(mod, "langchain.tools", "StructuredTool")
assert result is StructuredTool
# -- non-langchain modules should not fall back --
def test_no_fallback_for_non_langchain_module(self):
import os
with pytest.raises(ImportError, match="Cannot import name 'nonexistent'"):
_resolve_attribute(os, "os", "nonexistent")
def test_no_fallback_for_langchain_core(self):
"""langchain_core is not remapped to langchain_classic."""
import langchain_core.messages as mod
with pytest.raises((ImportError, AttributeError)):
_resolve_attribute(mod, "langchain_core.messages", "TotallyFakeClass")
# -- truly missing attributes should still raise --
def test_missing_attribute_in_both_raises(self):
import langchain.agents as mod
with pytest.raises((ImportError, AttributeError, ModuleNotFoundError)):
_resolve_attribute(mod, "langchain.agents", "CompletelyFakeClassName")
# ---------------------------------------------------------------------------
# create_class backwards compatibility (end-to-end through prepare_global_scope)
# ---------------------------------------------------------------------------
class TestLangchainClassicBackwardsCompat:
"""Test that old flows with pre-1.0 langchain imports still load."""
# -- removed modules (module-level fallback) --
def test_from_langchain_memory(self):
code = dedent("""
from langchain.memory import ConversationBufferMemory
from langflow.custom import Component
class Comp(Component):
def run(self):
return ConversationBufferMemory
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_from_langchain_schema(self):
code = dedent("""
from langchain.schema import AgentAction
from langflow.custom import Component
class Comp(Component):
def run(self):
return AgentAction
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_from_langchain_chains(self):
code = dedent("""
from langchain.chains.base import Chain
from langflow.custom import Component
class Comp(Component):
def run(self):
return Chain
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_from_langchain_callbacks(self):
code = dedent("""
from langchain.callbacks.base import BaseCallbackHandler
from langflow.custom import Component
class Comp(Component):
def run(self):
return BaseCallbackHandler
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_from_langchain_llms(self):
code = dedent("""
from langchain.llms.base import BaseLLM
from langflow.custom import Component
class Comp(Component):
def run(self):
return BaseLLM
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_from_langchain_prompts(self):
code = dedent("""
from langchain.prompts import PromptTemplate
from langflow.custom import Component
class Comp(Component):
def run(self):
return PromptTemplate
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_from_langchain_output_parsers(self):
code = dedent("""
from langchain.output_parsers import PydanticOutputParser
from langflow.custom import Component
class Comp(Component):
def run(self):
return PydanticOutputParser
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_from_langchain_text_splitter(self):
code = dedent("""
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langflow.custom import Component
class Comp(Component):
def run(self):
return RecursiveCharacterTextSplitter
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_from_langchain_document_loaders(self):
code = dedent("""
from langchain.document_loaders.base import BaseLoader
from langflow.custom import Component
class Comp(Component):
def run(self):
return BaseLoader
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_from_langchain_retrievers(self):
code = dedent("""
from langchain.retrievers import ContextualCompressionRetriever
from langflow.custom import Component
class Comp(Component):
def run(self):
return ContextualCompressionRetriever
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_from_langchain_vectorstores(self):
code = dedent("""
from langchain.vectorstores.base import VectorStore
from langflow.custom import Component
class Comp(Component):
def run(self):
return VectorStore
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
# -- existing modules with removed attributes (attribute-level fallback) --
def test_from_langchain_agents_agent_executor(self):
code = dedent("""
from langchain.agents import AgentExecutor
from langflow.custom import Component
class Comp(Component):
def run(self):
return AgentExecutor
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_from_langchain_tools_structured_tool(self):
code = dedent("""
from langchain.tools import StructuredTool
from langflow.custom import Component
class Comp(Component):
def run(self):
return StructuredTool
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
# -- multiple imports from the same removed module --
def test_multiple_imports_from_removed_module(self):
code = dedent("""
from langchain.schema import AgentAction, AgentFinish
from langflow.custom import Component
class Comp(Component):
def run(self):
return AgentAction, AgentFinish
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
# -- mixing old and new imports in the same component --
def test_mixed_old_and_new_imports(self):
code = dedent("""
from langchain.agents import create_react_agent
from langchain.memory import ConversationBufferMemory
from langflow.custom import Component
class Comp(Component):
def run(self):
return create_react_agent, ConversationBufferMemory
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
# -- langchain 1.0 native imports still work --
def test_langchain_1_0_agents_import(self):
code = dedent("""
from langchain.agents import create_react_agent
from langflow.custom import Component
class Comp(Component):
def run(self):
return create_react_agent
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
def test_langchain_1_0_tools_import(self):
code = dedent("""
from langchain.tools import tool
from langflow.custom import Component
class Comp(Component):
def run(self):
return tool
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"
# -- langchain_core imports are not affected --
def test_langchain_core_import_unaffected(self):
code = dedent("""
from langchain_core.messages import HumanMessage
from langflow.custom import Component
class Comp(Component):
def run(self):
return HumanMessage
""")
result = create_class(code, "Comp")
assert result.__name__ == "Comp"