fix: resolve N817 lint error by using explicit imports from defusedxml

Replace 'import defusedxml.ElementTree as ET' with explicit imports
(fromstring, ParseError, Element) to satisfy ruff N817 rule that flags
CamelCase imported as acronym.

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2026-03-14 05:28:07 +00:00
parent 506155b4f4
commit ee55628db8
3 changed files with 8 additions and 8 deletions

View File

@@ -6,9 +6,10 @@ from typing import ClassVar
import urllib.error
import urllib.parse
import urllib.request
from xml.etree.ElementTree import Element
from crewai.tools import BaseTool, EnvVar
import defusedxml.ElementTree as ET
from defusedxml.ElementTree import fromstring
from pydantic import BaseModel, ConfigDict, Field
@@ -90,7 +91,7 @@ class ArxivPaperTool(BaseTool):
logger.error(f"Error fetching data from Arxiv: {e}")
raise
root = ET.fromstring(data)
root = fromstring(data)
papers = []
for entry in root.findall(self.ATOM_NAMESPACE + "entry"):
@@ -121,11 +122,11 @@ class ArxivPaperTool(BaseTool):
return papers
@staticmethod
def _get_element_text(entry: ET.Element, element_name: str) -> str | None:
def _get_element_text(entry: Element, element_name: str) -> str | None:
elem = entry.find(f"{ArxivPaperTool.ATOM_NAMESPACE}{element_name}")
return elem.text.strip() if elem is not None and elem.text else None
def _extract_pdf_url(self, entry: ET.Element) -> str | None:
def _extract_pdf_url(self, entry: Element) -> str | None:
for link in entry.findall(self.ATOM_NAMESPACE + "link"):
if link.attrib.get("title", "").lower() == "pdf":
return link.attrib.get("href")

View File

@@ -196,5 +196,4 @@ class TestXMLLoader:
"""Verify that ArxivPaperTool imports from defusedxml, not xml.etree.ElementTree."""
import crewai_tools.tools.arxiv_paper_tool.arxiv_paper_tool as arxiv_module
ET = arxiv_module.ET
assert "defusedxml" in ET.__name__
assert "defusedxml" in arxiv_module.fromstring.__module__

View File

@@ -1,7 +1,7 @@
from pathlib import Path
from unittest.mock import MagicMock, patch
import urllib.error
import defusedxml.ElementTree as ET
from defusedxml.ElementTree import ParseError
from crewai_tools import ArxivPaperTool
import pytest
@@ -108,7 +108,7 @@ def test_invalid_xml_response(mock_urlopen, tool):
mock_response.status = 200
mock_urlopen.return_value.__enter__.return_value = mock_response
with pytest.raises(ET.ParseError):
with pytest.raises(ParseError):
tool.fetch_arxiv_data("quantum", 1)