Monday, January 25, 2021

Friendly-contest: we have a winner!

 Since my last post, no new issue has been filed. As the deadline has passed (8 am, AST), I have written a short program to randomly draw a winner. In my last post, I listed incorrectly the entries which I double-checked prior to writing the program, which I tested a few times before the deadline. 

The program I wrote was not the most efficient, but should be easy to understand: I created a list with one item for each valid contest entries, shuffled it and picked the first item on the list as the possible "winner". Just to ensure that I didn't make any silly mistake, I did 100,000 random draws and compared with the original distribution.

The very last of these random draws was determined to be the winner.

Here's the program:

from random import shuffle

entries = {
    "Dominik1123": 19,
    "sdementen": 6,
    "gdementen": 3,
    "tomerv": 5,
    "dcambie": 3,
    "carreau": 1,
}
results = {
    "Dominik1123": 0,
    "sdementen": 0,
    "gdementen": 0,
    "tomerv": 0,
    "dcambie": 0,
    "carreau": 0,
}

tickets = []
for name in entries:
    for number in range(entries[name]):
        tickets.append(name)

nb_trials = 100_000
rescale = len(tickets) / nb_trials

for i in range(nb_trials):
    shuffle(tickets)
    results[tickets[0]] += 1

for name in results:
    results[name] *= rescale


print("entries:", entries)
print("draws  :", results)
print("The winner is:", tickets[0])


And the winner is Dominik1123.


Thanks to every one who filed an issue for the contest, or simply tried Friendly-traceback.

Sunday, January 24, 2021

Friendly-contest: 20 hours left

There is only 20 hours left in the Friendly-traceback contest: write bad code to win a prize. After a slow start, there has been quite a few submissions lately which will definitely help to improve Friendly-traceback. Some submissions included references to StackOverflow questions and were thus determined to be worth two entries.  Currently, the number of contest entries stands as follows (using Github usernames):

  • Dominik1123: 19
  • sdementen: 6
  • gdementen: 3
  • tomerv: 3
  • dcambie: 2
  • carreau: 1

The draw will be made randomly so that, while people having more entries have a better chance of winning, anyone with a valid contest entry could win.


Saturday, January 23, 2021

Friendly contest: two days left after a surge of submissions

This is just a quick update.

Yesterday, the number of valid entries jumped from 9 to 23. Many of them have given me ideas on how to make Friendly-traceback better at finding the cause of the error but a few of them are likely going to be impossible for Friendly-traceback to evaluate properly and zero in on the exact cause of the exception. 

Friday, January 22, 2021

Contest: 3 submitters, 3x3 entries, 3 days left

 The contest I announced for Friendly-traceback has resulted in a total of  nine entries so far from three different programmers.  Two of the cases submitted have already been fixed in the development version. They also gave me some ideas to explore other possible cases and I found a few additional ones that need to be fixed.

Meanwhile, on Twitter, I saw a few discussions (for example, here and here) about improvements to messages given by the Python parser for SyntaxError cases, and comparisons with Pypy which currently does a better job in some cases in providing more useful error messages. However, as recorded in this issue on the Python tracker, doing this in a useful way can be quite challenging:


Strangely enough, the apparent impossibility of giving useful error messages in some cases reassures me since I couldn't figure out an approach that would always give the right clues. Perhaps I need to worry less and keep at it with my ad-hoc approach, looking for small improvements. Like Voltaire wrote: Le mieux est l'ennemi du bien  (Perfect is the enemy of good).

Monday, January 18, 2021

Don't you want to win a free book?

 At the end of Day 2 of the contest, still only one entry. If this keeps up, by next Monday there will not be a draw for a prize, and we will have a winner by default.


The submission was based on the use of __slots__. In playing around with similar cases, I found an AttributeError message that I had not seen before.  Here's a sample code.

class F:
__slots__ = ["a"]
b = 1

f = F()
f.b = 2

What happens if I execute this code using Friendly-traceback. Normally, there would be an explanation provided below the list of variables. Here we see nothing.



Let's inspect by using the friendly console.





I'll have to take care of this later today. Perhaps you know of other error messages specific to the use of __slots__. If so, and if you are quick enough, you could enter the contest. ;-)

Sunday, January 17, 2021

Friendly contest: the race is on

tl; dr: Python was wrong ;-)


After one day, I've had one valid entry submitted to the contest I announced yesterday; I've also had two other submissions from the same contributor that I deemed to be invalid for the contest. The submissions shared a similar characteristics to a different degree: the information provided by Python in the exception message did not tell the whole story and, taken on its own, might have been considered to be misleading. 

One such cases which I did not considered to be valid for this contest was the error message UnboundLocalError: local variable 'x' referenced before assignment  given for code containing the following

def f():
    x = 1
    del x
    print(x)  # raises the exception

When the exception is raised, there is indeed no trace of variable "x". So while there was an assignment for such a variable before, after deletion it no longer exists. Reading this code, instead of an UnboundLocalError, the exception that should probably be raised at this point is NameError: name 'x' is not defined ; however, Friendly-traceback's role is not to second-guess Python but to explain what an exception means and, whenever possible, give more details using the information available when the exception was raised. I considered this case and another one to be beyond the scope of what Friendly-traceback could accomplish.

The entry that I deemed to be valid was based on the following code:

class F:
    __slots__ = ["a"]

f = F()
f.b = 1

The error message given by Python in this case is AttributeError: 'F' object has no attribute 'b'. While technically correct, the problem is not that this object has no such attribute but that it cannot have such an attribute. This information can be easily obtained when the exception is raised and the information provided by Friendly-traceback now includes the following:

The object f has no attribute named b. 
Note that object f uses __slots__ which prevents the creation of new 
attributes. The following are some of its known attributes: a.

Reminder: the contest is open for 8 more days.




Saturday, January 16, 2021

Write bad code to win a prize

 

Summary

Get a chance of winning a prize by writing code with ONE error that Friendly-traceback cannot properly analyze, in one of three categories:

  • SyntaxError: invalid syntax
  • SyntaxError: some message, where some message is not recognized.
  • Any case of NameError, AttributeError, ModuleNotFoundError, UnboundLocalError, ImportError, IndexError, KeyError that is not recognized or is given an incorrect explanation by Friendly-traceback.

Submitted issues about bugs for Friendly-traceback itself are also considered for this contest.

Links: Github issue

Friendly-traceback documentation

The prize

There will be one prize given drawn randomly from all eligible submissions. The prize consists of one ebook/pbook of your choice with a maximum value of 50 USD (including delivery for pbook) as long as I can order it and have it delivered to you. Alternatively, a donation for that amount to the open source project of your choice if it can be made using Paypal.

The details

Each valid issue will get one entry for the contest. Valid issues contain code that might be expected to be written by a beginner or advanced beginner. It excludes code that uses type annotations as well as the use of async and await keywords.  The code is expected to contain ONE mistake only and not generate secondary exceptions.

The code can be run either using the friendly-console, or running in a Jupyter environment or from an editor as described in the documentation.

For a given valid submission, a bonus entry will be given if a link can be provided to an actual example from a site (such as StackOverflow, /r/python or /r/learnpython, etc.) where a question had been asked prior to this contest.

Exceptions that are not recognized by Friendly-traceback or for which the explanation (in English or French) is wrong or misleading are considered to be valid issues.

Submissions that are considered to be duplicate of previously submitted cases (because they basically have the same cause) will not be considered.

Honor code

I would ask that you do not read the source of Friendly-traceback with the sole intent of finding ways to write code that is designed to lead it to provide incorrect explanations.

End of contest

The contest will end on Monday January 25, at 8 AM Atlantic Standard Time.