| 1 |
import sys |
| 2 |
|
| 3 |
import friendly_traceback |
| 4 |
|
| 5 |
# TODO: make sure that all these cases are captured in the documentation |
| 6 |
|
| 7 |
# Need to add more cases, ensuring that correct object is always identified |
| 8 |
|
| 9 |
# a = [1, 2, 3] |
| 10 |
# b = 3, 4, 5 |
| 11 |
|
| 12 |
# b[0] > a.max --> max(a) |
| 13 |
# b.min == a[0] --> min(b) |
| 14 |
# a.length --> len(a) |
| 15 |
# a.len --> len(a) |
| 16 |
|
| 17 |
# a.b -> a, b ? |
| 18 |
|
| 19 |
|
| 20 |
def test_Generic(): |
| 21 |
# Generic - no additional explanation |
| 22 |
class A: |
| 23 |
pass |
| 24 |
|
| 25 |
try: |
| 26 |
A.x # testing type |
| 27 |
except AttributeError as e: |
| 28 |
message = str(e) |
| 29 |
friendly_traceback.explain_traceback(redirect="capture") |
| 30 |
result = friendly_traceback.get_output() |
| 31 |
|
| 32 |
assert "AttributeError: type object 'A' has no attribute 'x'" in result |
| 33 |
if friendly_traceback.get_lang() == "en": |
| 34 |
assert "The object `A` has no attribute" in result |
| 35 |
if friendly_traceback._writing_docs: |
| 36 |
return result, message |
| 37 |
|
| 38 |
|
| 39 |
def test_Generic_different_frame(): |
| 40 |
class A: |
| 41 |
attr = 1 |
| 42 |
|
| 43 |
def f(): |
| 44 |
class A: |
| 45 |
attr2 = 1 |
| 46 |
return A() |
| 47 |
|
| 48 |
a = f() |
| 49 |
try: |
| 50 |
a.attr |
| 51 |
except AttributeError as e: |
| 52 |
message = str(e) |
| 53 |
friendly_traceback.explain_traceback(redirect="capture") |
| 54 |
result = friendly_traceback.get_output() |
| 55 |
|
| 56 |
assert "AttributeError: 'A' object has no attribute 'attr'" in result |
| 57 |
if friendly_traceback.get_lang() == "en": |
| 58 |
assert "The object `a` has no attribute" in result |
| 59 |
assert "Did you mean `attr2`" in result |
| 60 |
if friendly_traceback._writing_docs: |
| 61 |
return result, message |
| 62 |
|
| 63 |
|
| 64 |
def test_Generic_instance(): |
| 65 |
class A: |
| 66 |
pass |
| 67 |
a = A() |
| 68 |
try: |
| 69 |
a.x |
| 70 |
except AttributeError as e: |
| 71 |
message = str(e) |
| 72 |
friendly_traceback.explain_traceback(redirect="capture") |
| 73 |
result = friendly_traceback.get_output() |
| 74 |
|
| 75 |
assert "AttributeError: 'A' object has no attribute 'x'" in result |
| 76 |
if friendly_traceback.get_lang() == "en": |
| 77 |
assert "The object `a` has no attribute" in result |
| 78 |
if friendly_traceback._writing_docs: |
| 79 |
return result, message |
| 80 |
|
| 81 |
|
| 82 |
def test_Object_attribute_typo(): |
| 83 |
# |
| 84 |
try: |
| 85 |
a = [1, 2, 3] |
| 86 |
a.appendh(4) |
| 87 |
except AttributeError as e: |
| 88 |
message = str(e) |
| 89 |
friendly_traceback.explain_traceback(redirect="capture") |
| 90 |
result = friendly_traceback.get_output() |
| 91 |
|
| 92 |
assert "AttributeError: 'list' object has no attribute 'appendh'" in result |
| 93 |
if friendly_traceback.get_lang() == "en": |
| 94 |
assert "Did you mean `append`" in result |
| 95 |
if friendly_traceback._writing_docs: |
| 96 |
return result, message |
| 97 |
|
| 98 |
|
| 99 |
def test_Use_builtin(): |
| 100 |
# |
| 101 |
try: |
| 102 |
a = [1, 2, 3] |
| 103 |
a.length() |
| 104 |
except AttributeError as e: |
| 105 |
message = str(e) |
| 106 |
friendly_traceback.explain_traceback(redirect="capture") |
| 107 |
result = friendly_traceback.get_output() |
| 108 |
|
| 109 |
assert "AttributeError: 'list' object has no attribute 'length'" in result |
| 110 |
if friendly_traceback.get_lang() == "en": |
| 111 |
assert "Did you mean `len(a)`" in result |
| 112 |
if friendly_traceback._writing_docs: |
| 113 |
return result, message |
| 114 |
|
| 115 |
|
| 116 |
def test_Use_synonym(): |
| 117 |
# |
| 118 |
try: |
| 119 |
a = [1, 2, 3] |
| 120 |
a.add(4) |
| 121 |
except AttributeError as e: |
| 122 |
message = str(e) |
| 123 |
friendly_traceback.explain_traceback(redirect="capture") |
| 124 |
result = friendly_traceback.get_output() |
| 125 |
|
| 126 |
assert "AttributeError: 'list' object has no attribute 'add'" in result |
| 127 |
if friendly_traceback.get_lang() == "en": |
| 128 |
assert "Did you mean `append`" in result |
| 129 |
if friendly_traceback._writing_docs: |
| 130 |
return result, message |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
def test_Module_attribute_typo(): |
| 135 |
import string |
| 136 |
|
| 137 |
try: |
| 138 |
string.ascii_lowecase |
| 139 |
except AttributeError as e: |
| 140 |
friendly_traceback.explain_traceback(redirect="capture") |
| 141 |
result = friendly_traceback.get_output() |
| 142 |
|
| 143 |
assert "AttributeError: module 'string' has no attribute 'ascii_lowecase'" in result |
| 144 |
if friendly_traceback.get_lang() == "en": |
| 145 |
assert "Did you mean `ascii_lowercase`" in result |
| 146 |
|
| 147 |
import math |
| 148 |
|
| 149 |
try: |
| 150 |
math.cost |
| 151 |
except AttributeError as e: |
| 152 |
message = str(e) |
| 153 |
friendly_traceback.explain_traceback(redirect="capture") |
| 154 |
result = friendly_traceback.get_output() |
| 155 |
|
| 156 |
assert "AttributeError: module 'math' has no attribute 'cost'" in result |
| 157 |
if friendly_traceback.get_lang() == "en": |
| 158 |
assert ( |
| 159 |
"Instead of writing `math.cost`, perhaps you meant to write one of" |
| 160 |
in result |
| 161 |
) |
| 162 |
assert "cos, cosh" in result or "cosh, cos" in result |
| 163 |
assert not "acosh" in result |
| 164 |
if friendly_traceback._writing_docs: |
| 165 |
return result, message |
| 166 |
|
| 167 |
|
| 168 |
def test_Shadow_stdlib_module(): |
| 169 |
import turtle |
| 170 |
|
| 171 |
try: |
| 172 |
turtle.Pen |
| 173 |
except AttributeError as e: |
| 174 |
message = str(e) |
| 175 |
friendly_traceback.explain_traceback(redirect="capture") |
| 176 |
result = friendly_traceback.get_output() |
| 177 |
|
| 178 |
assert "AttributeError: module 'turtle' has no attribute 'Pen'" in result |
| 179 |
if friendly_traceback.get_lang() == "en": |
| 180 |
assert ( |
| 181 |
"There is also a module named `turtle` in Python's standard library." |
| 182 |
in result |
| 183 |
) |
| 184 |
if friendly_traceback._writing_docs: |
| 185 |
return result, message |
| 186 |
|
| 187 |
|
| 188 |
def test_Nonetype(): |
| 189 |
a = None |
| 190 |
try: |
| 191 |
a.b |
| 192 |
except AttributeError as e: |
| 193 |
message = str(e) |
| 194 |
friendly_traceback.explain_traceback(redirect="capture") |
| 195 |
result = friendly_traceback.get_output() |
| 196 |
|
| 197 |
assert "'NoneType' object has no attribute 'b'" in result |
| 198 |
if friendly_traceback.get_lang() == "en": |
| 199 |
assert "for a variable whose value is `None`" in result |
| 200 |
|
| 201 |
if friendly_traceback._writing_docs: |
| 202 |
return result, message |
| 203 |
|
| 204 |
|
| 205 |
def test_Perhaps_comma(): |
| 206 |
import sys |
| 207 |
abcd = "hello" |
| 208 |
defg = "world" |
| 209 |
|
| 210 |
# fmt: off |
| 211 |
try: |
| 212 |
a = [abcd |
| 213 |
.defg] |
| 214 |
# fmt: on |
| 215 |
except AttributeError as e: |
| 216 |
message = str(e) |
| 217 |
friendly_traceback.explain_traceback(redirect="capture") |
| 218 |
result = friendly_traceback.get_output() |
| 219 |
assert "'str' object has no attribute 'defg'" in result |
| 220 |
if friendly_traceback.get_lang() == "en": |
| 221 |
assert "Did you mean to separate object names by a comma" in result |
| 222 |
if friendly_traceback._writing_docs: |
| 223 |
return result, message |
| 224 |
|
| 225 |
|
| 226 |
def test_Builtin_function(): |
| 227 |
text = 'Hello world!' |
| 228 |
try: |
| 229 |
len.text |
| 230 |
except AttributeError as e: |
| 231 |
message = str(e) |
| 232 |
friendly_traceback.explain_traceback(redirect="capture") |
| 233 |
result = friendly_traceback.get_output() |
| 234 |
|
| 235 |
assert "'builtin_function_or_method'" in result |
| 236 |
if friendly_traceback.get_lang() == "en": |
| 237 |
assert "Did you mean `len(text)`" in result |
| 238 |
if friendly_traceback._writing_docs: |
| 239 |
return result, message |
| 240 |
|
| 241 |
|
| 242 |
def test_Builtin_module_with_no_file(): |
| 243 |
"""Issue 116""" |
| 244 |
import sys |
| 245 |
|
| 246 |
try: |
| 247 |
sys.foo |
| 248 |
except AttributeError as e: |
| 249 |
message = str(e) |
| 250 |
friendly_traceback.explain_traceback(redirect="capture") |
| 251 |
result = friendly_traceback.get_output() |
| 252 |
|
| 253 |
assert "module 'sys' has no attribute 'foo'" in result |
| 254 |
if friendly_traceback.get_lang() == "en": |
| 255 |
assert "Python tells us" in result |
| 256 |
if friendly_traceback._writing_docs: |
| 257 |
return result, message |
| 258 |
|
| 259 |
|
| 260 |
def test_Using_slots(): |
| 261 |
"""Issue 141""" |
| 262 |
|
| 263 |
class F: |
| 264 |
__slots__ = ["a"] |
| 265 |
|
| 266 |
f = F() |
| 267 |
try: |
| 268 |
f.b = 1 |
| 269 |
except AttributeError as e: |
| 270 |
message = str(e) |
| 271 |
friendly_traceback.explain_traceback(redirect="capture") |
| 272 |
result = friendly_traceback.get_output() |
| 273 |
|
| 274 |
assert "'F' object has no attribute 'b'" in result |
| 275 |
if friendly_traceback.get_lang() == "en": |
| 276 |
assert "object `f` uses `__slots__`" in result |
| 277 |
if friendly_traceback._writing_docs: |
| 278 |
return result, message |
| 279 |
|
| 280 |
|
| 281 |
def test_Read_only(): |
| 282 |
|
| 283 |
class F: |
| 284 |
__slots__ = ["a"] |
| 285 |
b = 2 |
| 286 |
|
| 287 |
f = F() |
| 288 |
try: |
| 289 |
f.b = 1 |
| 290 |
except AttributeError as e: |
| 291 |
message = str(e) |
| 292 |
friendly_traceback.explain_traceback(redirect="capture") |
| 293 |
result = friendly_traceback.get_output() |
| 294 |
|
| 295 |
assert "'F' object attribute 'b' is read-only" in result |
| 296 |
if friendly_traceback.get_lang() == "en": |
| 297 |
assert "The only attribute of `f` whose value can be changed is" in result |
| 298 |
if friendly_traceback._writing_docs: |
| 299 |
return result, message |
| 300 |
|
| 301 |
|
| 302 |
def test_Tuple_by_accident(): |
| 303 |
something = "abc", # note trailing comma |
| 304 |
try: |
| 305 |
something.upper() |
| 306 |
except AttributeError as e: |
| 307 |
message = str(e) |
| 308 |
friendly_traceback.explain_traceback(redirect="capture") |
| 309 |
result = friendly_traceback.get_output() |
| 310 |
|
| 311 |
assert "'tuple' object has no attribute 'upper'" in result |
| 312 |
if friendly_traceback.get_lang() == "en": |
| 313 |
assert "Did you write a comma" in result |
| 314 |
|
| 315 |
if friendly_traceback._writing_docs: |
| 316 |
return result, message |
| 317 |
|
| 318 |
|
| 319 |
def test_Attribute_from_other_module(): |
| 320 |
import math |
| 321 |
import keyword |
| 322 |
|
| 323 |
try: |
| 324 |
keyword.pi |
| 325 |
except AttributeError as e: |
| 326 |
message = str(e) |
| 327 |
friendly_traceback.explain_traceback(redirect="capture") |
| 328 |
|
| 329 |
result = friendly_traceback.get_output() |
| 330 |
assert "module 'keyword' has no attribute 'pi'" in result |
| 331 |
if friendly_traceback.get_lang() == "en": |
| 332 |
assert "Did you mean `math`?" in result |
| 333 |
|
| 334 |
import cmath |
| 335 |
try: |
| 336 |
keyword.pi |
| 337 |
except AttributeError as e: |
| 338 |
message = str(e) |
| 339 |
friendly_traceback.explain_traceback(redirect="capture") |
| 340 |
|
| 341 |
result = friendly_traceback.get_output() |
| 342 |
if friendly_traceback.get_lang() == "en": |
| 343 |
assert "Did you mean one of the following modules:" in result |
| 344 |
|
| 345 |
if friendly_traceback._writing_docs: |
| 346 |
return result, message |
| 347 |
|
| 348 |
|
| 349 |
def test_Use_join_with_str(): |
| 350 |
try: |
| 351 |
a = ['a', '2'].join('abc') + ['b', '3'].join('\n') |
| 352 |
except AttributeError as e: |
| 353 |
message = str(e) |
| 354 |
friendly_traceback.explain_traceback(redirect="capture") |
| 355 |
|
| 356 |
result = friendly_traceback.get_output() |
| 357 |
assert "'list' object has no attribute 'join'" in result |
| 358 |
if friendly_traceback.get_lang() == "en": |
| 359 |
assert "something like `'abc'.join(['a', '2'])`" in result |
| 360 |
if friendly_traceback._writing_docs: |
| 361 |
return result, message |
| 362 |
|
| 363 |
|
| 364 |
def test_Circular_import(): |
| 365 |
from friendly_traceback.runtime_errors import stdlib_modules |
| 366 |
stdlib_modules.names.add("my_turtle1") |
| 367 |
try: |
| 368 |
import my_turtle1 |
| 369 |
except AttributeError as e: |
| 370 |
message = str(e) |
| 371 |
friendly_traceback.explain_traceback(redirect="capture") |
| 372 |
|
| 373 |
result = friendly_traceback.get_output() |
| 374 |
# Different messages for python < 3.8 |
| 375 |
assert ( "partially initialized module 'my_turtle1'" in result or |
| 376 |
"module 'my_turtle1' has no attribute 'something'" in result) |
| 377 |
if friendly_traceback.get_lang() == "en": |
| 378 |
assert "import a module with the same name" in result |
| 379 |
assert "from Python's standard library" in result |
| 380 |
stdlib_modules.names.pop() |
| 381 |
if friendly_traceback._writing_docs: |
| 382 |
return result, message |
| 383 |
|
| 384 |
def test_Circular_import_b(): |
| 385 |
try: |
| 386 |
import circular_c |
| 387 |
except AttributeError as e: |
| 388 |
message = str(e) |
| 389 |
friendly_traceback.explain_traceback(redirect="capture") |
| 390 |
|
| 391 |
result = friendly_traceback.get_output() |
| 392 |
|
| 393 |
assert ( |
| 394 |
"module 'circular_c' has no attribute 'something'" in result or |
| 395 |
"partially initialized module 'circular_c' has no attribute 'something'" |
| 396 |
in result |
| 397 |
) |
| 398 |
if friendly_traceback.get_lang() == "en": |
| 399 |
assert "have a circular import." in result |
| 400 |
if friendly_traceback._writing_docs: |
| 401 |
return result, message |
| 402 |
|
| 403 |
|
| 404 |
if __name__ == "__main__": |
| 405 |
print(test_Generic()[0]) |