Skip to content

bpo-39999: Improve compatibility of the ast module.#19056

Merged
serhiy-storchaka merged 5 commits intopython:masterfrom
serhiy-storchaka:ast-classes
Mar 22, 2020
Merged

bpo-39999: Improve compatibility of the ast module.#19056
serhiy-storchaka merged 5 commits intopython:masterfrom
serhiy-storchaka:ast-classes

Conversation

@serhiy-storchaka
Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka commented Mar 18, 2020

  • Re-add removed classes Suite, Param, AugLoad and AugStore.
  • Add docstrings for dummy classes.
  • Add docstrings for attribute aliases.
  • Set __module__ to "ast" instead of "_ast".

https://bugs.python.org/issue39999

* Re-add removed classes Suite, Param, AugLoad and AugStore.
* Add docstrings for dummy classes.
* Add docstrings for attribute aliases.
* Set __module__ to "ast" instead of "_ast".
Comment thread Lib/ast.py Outdated
Comment on lines +587 to +598

class Suite(mod):
"""Unused AST node class."""

class AugLoad(expr_context):
"""Unused AST node class."""

class AugStore(expr_context):
"""Unused AST node class."""

class Param(expr_context):
"""Unused AST node class"""
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we deprecate this as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used word "deprecated" if it was used in past Python releases and the user code can use them for compatibility. I used word "unused" if it was not used in Python (at least in Python 3) and there is no reason to use it in the user code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But aren't them will be removed in time or stay them forever? If they will, wouldn't it be good to start doing warnings?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to start emitting warnings when the user code can just drop these classes. Currently Param is needed for 2.7, and some projects still support it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently Param is needed for 2.7, and some projects still support it.

Oh, that makes sense.

@pablogsal pablogsal changed the title bpo-39999: Imporove compatibility of the ast module. bpo-39999: Improve compatibility of the ast module. Mar 19, 2020
@asottile asottile mentioned this pull request Mar 19, 2020
Comment thread Lib/ast.py Outdated
Constant.n = property(_getter, _setter)
Constant.s = property(_getter, _setter)
Constant.n = property(_getter, _setter, doc='Deprecated. Use value instead.')
Constant.s = property(_getter, _setter, doc='Deprecated. Use value instead.')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to emit a DeprecationWarning when it's used?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is possible. But it would be better to start emitting warnings when the user code no longer need to use the deprecated feature. I.e. when the support of Python 3.7 be dropped in most projects which use AST.

Comment thread Lib/ast.py
}

class Index(AST):
class slice(AST):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@serhiy-storchaka what do you think about implementing custom instance and subclass checks for slice? slice is a sum type, so there should be checks like this issubclass(thing, ast.slice) or isinstance(thing, ast.slice)

something like this should work

    def __subclasscheck__(self, subclass):
        if issubclass(subclass, Slice):
            return True
        else:
            return super().__subclasscheck__(subclass)

    def __instancecheck__(self, instance):
        if isinstance(instance, Slice):
            return True
        else:
            return super().__instancecheck__(instance)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is Slice?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the simplification, it was part of slice kind. Now it is an expression by it is own but if we want to increase compatibility, wouldn't it better to it behave like a subclass of ast.slice?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think it would be useful because Index() can return arbitrary node type and it is impossible to distinguish ExtSlice from Tuple.

@serhiy-storchaka serhiy-storchaka merged commit bace59d into python:master Mar 22, 2020
@serhiy-storchaka serhiy-storchaka deleted the ast-classes branch March 22, 2020 18:33
colesbury referenced this pull request in colesbury/nogil Oct 6, 2021
* Re-add removed classes Suite, slice, Param, AugLoad and AugStore.
* Add docstrings for dummy classes.
* Add docstrings for attribute aliases.
* Set __module__ to "ast" instead of "_ast".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants