Comment 1 for bug 2065120

Revision history for this message
Chris Papademetrious (chrispitude) wrote :

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...