最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Weird Python Error "TypeError: an integer is required" on a List - Only in Debugger - Fixed by adding print -

matteradmin8PV0评论

I have a rather strange error in the following code:

CUSTOMER_ORDER = ["Some", "Customers"]
ticket_types = ["analysis_tickets"] # is list because copied from legacy code and only need 1 type here (TODO replace with str.)

[...]

# Get all  tickets of each type for each customer of the month
for customer in CUSTOMER_ORDER:
    all_tickets[customer] = {}

    for ticket_type in ticket_types:
        logger.info("Getting '" + ticket_type + "' for customer " + customer + "...")

        if ticket_type == "analysis_tickets":
            suffix = ANALYSIS_TICKETS_SUFFIX
        elif ticket_type == "support_tickets":
            suffix = SUPPORT_TICKETS_SUFFIX
        elif ticket_type == "engineering_tickets":
            suffix = ENGINEERING_TICKETS_SUFFIX

        all_tickets[customer][ticket_type] = {}
        all_tickets[customer][ticket_type]["tickets"] = []

        try:
            response = requests.get(
                OTRS_URL + "/TicketSearch",
                params={
                    "UserLogin": OTRS_USER,
                    "Password": OTRS_PW,
                    "Queues": customer + suffix,
                    time_older: datetime.datetime.now(),
                    time_newer: datetime.datetime.now() - datetime.timedelta(days=TIME_RANGE_DAYS),
                    "Limit": 3000,
                },
                verify=False,
            )
            if response.status_code != 200:
                logger.error("Could not get tickets for customer " + customer + ". Error: " + str(response.status_code))
                sys.exit(1)

            if "TicketID" not in response.json():
                logger.warning("No tickets found for customer " + customer + " amd ticket type '" + ticket_type + "'.")
                all_tickets[customer][ticket_type]["tickets"] = []
            else:
                all_tickets[customer][ticket_type]["tickets"] = response.json()["TicketID"]
                logger.info("Found " + str(len(all_tickets[customer][ticket_type]["tickets"])) + " tickets for customer " + customer + " and ticket type '" + ticket_type + "'.")
                ticket_count += len(all_tickets[customer][ticket_type]["tickets"])

            # print(ticket_type + " " + ticket_types)
        except Exception as e:
            logger.error("Could not get tickets for customer " + customer + ". Error: " + traceback.format_exc())
            sys.exit(1)

Error:

Code errors: Exception has occurred: TypeError
an integer is required  File "C:\Users\user\Documents\GitLab\various-scripts\OTRS Auto-Reporting\Analysts-Locked\otrs_reporting_analysts_locked.py", line 160, in <module>
    for ticket_type in ticket_types:
                       ^^^^^^^^^^^^
TypeError: an integer is required

However I was able to fix the issue by either:

  • uncomment the print statement at the end of the try block
  • removing the try/except block
  • running the script directly without debugger

And now I am completely confused. Why is the print statement doing anything? Why does it error with the debugger but works fine without? What has all this to do with an expected integer?

Can anyone make sense of this?

I am using Python 3.12.7 64 bit ( Microsoft Store) and the respective VS-Code plugin.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far