GithubHelp home page GithubHelp logo

Comments (5)

jakkdl avatar jakkdl commented on June 3, 2024 1

Oh, I totally thought I tested it with native - but apparently not. Sweet!

from libcst.

zsol avatar zsol commented on June 3, 2024 1

Gonna close this out as native parser is enabled by default in 1.0.0

from libcst.

jakkdl avatar jakkdl commented on June 3, 2024

So, I'm looking at whether to place this validation in BaseAssignTargetExpression, AssignTarget, and/or Assign/AnnAssign/AugAssign.

class BaseAssignTargetExpression(BaseExpression, ABC):
"""
An expression that's valid on the left side of an assignment. That assignment may
be part an :class:`Assign` node, or it may be part of a number of other control
structures that perform an assignment, such as a :class:`For` loop.
Python's grammar defines all expression as valid in this position, but the AST
compiler further restricts the allowed types, which is what this type attempts to
express.
This is similar to a :class:`BaseDelTargetExpression`, but it also includes
:class:`StarredElement` as a valid node.
The set of valid nodes are defined as part of `CPython's AST context computation
<https://github.com/python/cpython/blob/v3.8.0a4/Python/ast.c#L1120>`_.
"""

since 3.9 the python grammar does define the valid assignment targets

 assignment:
    | NAME ':' expression ['=' annotated_rhs ] 
    | ('(' single_target ')' 
         | single_subscript_attribute_target) ':' expression ['=' annotated_rhs ] 
    | (star_targets '=' )+ (yield_expr | star_expressions) !'=' [TYPE_COMMENT] 
    | single_target augassign ~ (yield_expr | star_expressions) 

so I thought I'd add a _validate to BaseAssignTargetExpression that checks whether it's a Name, Subscript or StarredElement. But then I noticed that a lot of other classes inherits from it, including Name, Annotation, Attribute - which all quickly starts breaking if I restrict what BaseAssignTargetExpression can be. Is there a reason why all of these don't just inherit from BaseExpression, or can I change them to do so? Or should I place the validation in AssignTarget instead?

from libcst.

jakkdl avatar jakkdl commented on June 3, 2024

@zsol any thoughts on the above?

from libcst.

zsol avatar zsol commented on June 3, 2024

The native parser correctly raises a syntax error here. So to work around you can set the LIBCST_PARSER_TYPE environment variable to native before calling libcst, or wait for the next release which will have this behavior by default due to #929

from libcst.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.