[Python-Dev] Proposal: go back to enabling DeprecationWarning by default

Random832 random832 at fastmail.com
Fri Nov 10 11:02:43 EST 2017

On Tue, Nov 7, 2017, at 07:22, Nick Coghlan wrote:
> My suggestion for that definition is to have the *default* meaning of
> "third party code" be "everything that isn't __main__".

What is __main__? Or, rather, how do you determine when it is to blame?
For syntax it's easy, but any deprecated function necessarily belongs to
its own module and not to main. Main may have called it, which can be
detected from the stack trace, or it may have used it in some other way
(pass to some builtin or e.g. itertools function that takes a callable
argument, for example). Maybe the DeprecationWarning should be raised at
the name lookup* rather than the call? What if "calling this function
with some particular combination of arguments" is deprecated?

*i.e. something like:

class deprecated:
   def __init__(self, obj): self.obj = obj
class DeprecatableModule(ModuleType):
   def __getattr__(self, name):
      obj = self.__dict__[name]
      if isinstance(type(obj), deprecated):
          if (detect somehow caller is __main__): raise
          return obj.obj
      else: return obj
    def __dir__(self):
        return [k for k in self.__dict__ if not
        isinstance(self.__dict__[k], deprecated)]

def some_deprecated_function(...): ...


More information about the Python-Dev mailing list