1

I ran my program in Python 3.4, and it crashed. Stack trace:

Traceback (most recent call last):
  [...]

  File [...], line 176, in manual_setup
    gammas.add((name, group_oid))
KeyError: '5733455d-ba37-48c6-b550-8f53b719310c'

Here's the code at that line. Never mind what the variables are, just that gammas is a set, as you can see:

gammas = set()
for group_oid, name, _ in self.tags:
    gammas.add((name, group_oid))

By the way, name and group_oid are both str, but even if they were something unhashable, I'd get a different error.

I'm not excluding the possibility that I have something totally different going on, but before I look into weird causes I haven't even thought of yet, I'd like to know if set.add could possibly be throwing KeyError. The documentation suggests no. My knowledge of how sets work says it shouldn't. Has anyone out there seen this happen?

I checked to see if set was somehow overridden. PyCharm says it's the built-in Python set.

5
  • 2
    I agree that set.add itself should not raise a KeyError. What kind of object is self.tags? Any chance it does anything clever like lazy loading entries on iteration? Commented Aug 31, 2016 at 7:31
  • 1
    To hope to debug this you will need start with a blank file and create a minimal complete and verifiable example that shows this error. Otherwise there this bug can't be reproduced. Commented Aug 31, 2016 at 7:40
  • @DanielHepper self.tags is a list. To be sure, I'll print out the type just in case, along with group_oid and name, when I debug. Commented Aug 31, 2016 at 7:42
  • @JamesK I'll try putting those exact values into a set once I find the offending ones. Need to run one more time with debug output in that spot. Commented Aug 31, 2016 at 7:42
  • Make sure you're looking at the values from the iteration where the error occurs. Commented Aug 31, 2016 at 7:43

2 Answers 2

4

The only set operations that generate a KeyError are pop on an empty set and remove for an element that isn't in the set. add can't generate a KeyError.

My first guess would be that this exception is coming from the __hash__ method of name or group_oid. Inspecting those objects in a debugger could be informative. There's also the possibility it's coming from an __eq__ method.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'm checking on those right now. Will take some more time to run the script to that point again.
0

Somehow, the Python interpreter was outputting the wrong line number and accompanying code for the error. The error was a few lines above. I should've guessed... Maybe I changed the code while it was running (How can the line numbers in my stack traces be wrong?), but I don't think I did. Anyway, it's not happening anymore.

P.S. I didn't immediately re-run and catch it because the script takes a long time to reach that point again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.