Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7845285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:10:31+00:00 2026-06-02T17:10:31+00:00

Basic question: What’s the most Pythonic/logical way to make my own, custom warning classes?

  • 0

Basic question: What’s the most Pythonic/logical way to make my own, custom warning classes? What are the correct warning and exception classes that I should be subclassing?

Motivation: The requirements for the library I’m writing specify that if a MyContainer object c contains an item x and the caller of the library tries to place a “duplicate” of x — call it y — into c, a warning is issued to the caller and the return value of c.my_transformation_method(x, y) is placed into c to replace x. In other words, MyContainers will replace elements with their duplicates, but must warn the user when doing so.

Based on my reading, the most flexible way to warn the caller of a library about a nonfatal action is with the warnings standard module. It allows the caller to handle the warning as it sees fit, doing anything from ignoring warnings to treating them as errors. (Note that I’m using Python 3, but I don’t think that’s essential to the question here.)

Example: What I’ve done is defined the following warning subclass:

class DuplicateItemWarning(UserWarning, ValueError):
    pass

Then the add() method of MyContainer calls warnings.warn('detected duplicate', DuplicateItemWarning) when it detects an attempt to insert a duplicate item.

Specific questions:

  1. Should I be subclassing UserWarning as above, or just sublcassing Warning?

  2. It seems semantically sensible to subclass ValueError (which, in the above example, merely inserts ValueError in the MRO between Warning and Exception) in case a caller wants to treat warnings as errors. Is there a drawback to this I’m not seeing?

  3. I could find no previous questions on StackOverflow about customizing warning classes. Is this because Python programmers don’t even like using the warnings module?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:10:33+00:00Added an answer on June 2, 2026 at 5:10 pm

    After reading the PEP 230 about the warning framework and the warnings docs, I think I have the answer to your questions:

    1. UserWarning and all others are warning categories, they don’t seem to have another role than just classification. This way you could filter them out in your production environment for example. So, basically, you could subclass from Warning if the warning does not fall in any other category. If in the context, UserWarning or RuntimeWarning seem enough, just use them.

    2. Warnings are already Exceptions. So, technically, to “catch” them as errors, you just need to change the filter, no need to subclass from any XXXError. Now, again it’s all about making sense. If the warnings are about the values passed, you could subclass from ValueError, especially if there would be many different custom warnings, you would expect the caller to “catch” all warnings regarding values all at once.

      try:
           # do something
      except MyCustomWarningOne:
          # do something else
      except MyCustomWarningTwo:
          # do something else also
      except ValueError: # or RuntimeWarning if you subclass from it
          # catch some other warning (both of these subclass from ValueError for example)
      
    3. The warnings module is Guido van Rossum’s idea. (See PEP 230). If that ain’t Pythonic enough … 😀

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basic question about the way git works. I clone a repo, make some changes,
Basic question: Including a page, that contains a component with component id, multiple times
really basic question, I have a string to generate as SQL statement that I
My basic question is: How can I force Hibernate to make float NULLable and
I have a basic question that I can't seem to resolve using Google (Or
Really basic question here. So I'm told that a dot . matches any character
Very basic question: I am coding a web app that has a handful of
Very basic question, but I have an error in my code that can only
BASIC QUESTION: Is there a way to set the mime-type (content-type) of elements inside
Basic question: In R, how can I make a list and later populate it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.