code/source: remove support for comparing Source with str

Cross-type comparisons like this are a bad idea. This isn't used.
This commit is contained in:
Ran Benita 2020-07-01 20:20:10 +03:00
parent 2b99bfbc60
commit a127a22d13
3 changed files with 10 additions and 12 deletions

View File

@ -44,13 +44,10 @@ class Source:
else: else:
self.lines = deindent(getsource(obj).lines) self.lines = deindent(getsource(obj).lines)
def __eq__(self, other): def __eq__(self, other: object) -> bool:
try: if not isinstance(other, Source):
return self.lines == other.lines return NotImplemented
except AttributeError: return self.lines == other.lines
if isinstance(other, str):
return str(self) == other
return False
# Ignore type because of https://github.com/python/mypy/issues/4266. # Ignore type because of https://github.com/python/mypy/issues/4266.
__hash__ = None # type: ignore __hash__ = None # type: ignore

View File

@ -6,6 +6,7 @@ import pytest
from _pytest._code import Code from _pytest._code import Code
from _pytest._code import ExceptionInfo from _pytest._code import ExceptionInfo
from _pytest._code import Frame from _pytest._code import Frame
from _pytest._code import Source
from _pytest._code.code import ExceptionChainRepr from _pytest._code.code import ExceptionChainRepr
from _pytest._code.code import ReprFuncArgs from _pytest._code.code import ReprFuncArgs
@ -67,7 +68,7 @@ def test_getstatement_empty_fullsource() -> None:
f = Frame(func()) f = Frame(func())
with mock.patch.object(f.code.__class__, "fullsource", None): with mock.patch.object(f.code.__class__, "fullsource", None):
assert f.statement == "" assert f.statement == Source("")
def test_code_from_func() -> None: def test_code_from_func() -> None:

View File

@ -227,9 +227,9 @@ class TestSourceParsingAndCompiling:
''')""" ''')"""
) )
s = source.getstatement(0) s = source.getstatement(0)
assert s == str(source) assert s == source
s = source.getstatement(1) s = source.getstatement(1)
assert s == str(source) assert s == source
def test_getstatementrange_within_constructs(self) -> None: def test_getstatementrange_within_constructs(self) -> None:
source = Source( source = Source(
@ -445,7 +445,7 @@ def test_getsource_fallback() -> None:
expected = """def x(): expected = """def x():
pass""" pass"""
src = getsource(x) src = getsource(x)
assert src == expected assert str(src) == expected
def test_idem_compile_and_getsource() -> None: def test_idem_compile_and_getsource() -> None:
@ -454,7 +454,7 @@ def test_idem_compile_and_getsource() -> None:
expected = "def x(): pass" expected = "def x(): pass"
co = _pytest._code.compile(expected) co = _pytest._code.compile(expected)
src = getsource(co) src = getsource(co)
assert src == expected assert str(src) == expected
def test_compile_ast() -> None: def test_compile_ast() -> None: