I ran into an issue using _clone() in my own code. For whatever bizarre reason, code running inside a "pytest" test does not see the _clone() method.
For example, consider the following "test_clone.py" file:
==== #!/usr/bin/env python import bs4
# my own copy of _clone() def _myclone(self): clone = type(self)( None, None, self.name, self.namespace, self.prefix, self.attrs, is_xml=self._is_xml, sourceline=self.sourceline, sourcepos=self.sourcepos, can_be_empty_element=self.can_be_empty_element, cdata_list_attributes=self.cdata_list_attributes, preserve_whitespace_tags=self.preserve_whitespace_tags, interesting_string_types=self.interesting_string_types ) for attr in ('can_be_empty_element', 'hidden'): setattr(clone, attr, getattr(self, attr)) return clone
def test_foo(): body = bs4.BeautifulSoup('<body foo="bar"/>', 'lxml').find("body").extract() print(f'1: {body}') print(f'2: {_myclone(body)}') print(f"3: {type(body._clone)}") print(f'4: {body._clone()}')
test_foo() ====
If I run this script manually, it works as expected:
==== $ test_clone.py 1: <body foo="bar"></body> 2: <body foo="bar"></body> 3: <class 'method'> 4: <body foo="bar"></body> ====
But if I run it via pytest, the _clone() method is undefined:
==== ============== ERRORS ============== __ ERROR collecting test_clone.py __ test_clone.py:26: in <module> test_foo() test_clone.py:24: in test_foo print(f'4: {body._clone()}') E TypeError: 'NoneType' object is not callable --------- Captured stdout ---------- 1: <body foo="bar"></body> 2: <body foo="bar"></body> 3: <class 'NoneType'> ===== short test summary info ====== ERROR test_clone.py - TypeError: 'NoneType' object is not callable ====
It took me awhile to figure this out... and I still don't understand the why behind it...
I ran into an issue using _clone() in my own code. For whatever bizarre reason, code running inside a "pytest" test does not see the _clone() method.
For example, consider the following "test_clone.py" file:
====
#!/usr/bin/env python
import bs4
# my own copy of _clone()
self.prefix, self.attrs, is_xml= self._is_ xml,
sourceline= self.sourceline , sourcepos= self.sourcepos,
can_be_ empty_element= self.can_ be_empty_ element,
cdata_ list_attributes =self.cdata_ list_attributes ,
preserve_ whitespace_ tags=self. preserve_ whitespace_ tags,
interesting_ string_ types=self. interesting_ string_ types empty_element' , 'hidden'):
setattr( clone, attr, getattr(self, attr))
def _myclone(self):
clone = type(self)(
None, None, self.name, self.namespace,
)
for attr in ('can_be_
return clone
def test_foo(): up('<body foo="bar"/>', 'lxml') .find(" body"). extract( ) _clone) }")
body = bs4.BeautifulSo
print(f'1: {body}')
print(f'2: {_myclone(body)}')
print(f"3: {type(body.
print(f'4: {body._clone()}')
test_foo()
====
If I run this script manually, it works as expected:
====
$ test_clone.py
1: <body foo="bar"></body>
2: <body foo="bar"></body>
3: <class 'method'>
4: <body foo="bar"></body>
====
But if I run it via pytest, the _clone() method is undefined:
====
============== ERRORS ==============
__ ERROR collecting test_clone.py __
test_clone.py:26: in <module>
test_foo()
test_clone.py:24: in test_foo
print(f'4: {body._clone()}')
E TypeError: 'NoneType' object is not callable
--------- Captured stdout ----------
1: <body foo="bar"></body>
2: <body foo="bar"></body>
3: <class 'NoneType'>
===== short test summary info ======
ERROR test_clone.py - TypeError: 'NoneType' object is not callable
====
It took me awhile to figure this out... and I still don't understand the why behind it...