Coder Social home page Coder Social logo

Comments (12)

zhammer avatar zhammer commented on August 22, 2024 1

Sounds good to me! #37 is a fitting pr number :)

from libcst.

zhammer avatar zhammer commented on August 22, 2024 1

I'll update the PR title for better autocomplete if other folks search this in the future

from libcst.

DragonMinded avatar DragonMinded commented on August 22, 2024 1

This should be supported on master as of a few minutes ago. I don't have any real-world Python 3.5/3.6 that I can run this against easily. However, I've at least verified that the tokenizer works properly and all of our parsing/rendering tests have been updated with 3.6 and below versions as appropriate.

If you end up with time to give this a try and it works to your satisfaction, let me know!

from libcst.

zhammer avatar zhammer commented on August 22, 2024

Not sure if this is supported:

source_tree = cst.parse_module(python_source, config=cst.PartialParserConfig(python_version="3.6"))

Tried running but still got the async error.

from libcst.

zhammer avatar zhammer commented on August 22, 2024

Ah just saw this:

# `parse_version_string` will raise a ValueError if the version is invalid.
#
# We use object.__setattr__ because the dataclass is frozen. See:
# https://docs.python.org/3/library/dataclasses.html#frozen-instances
# This should be safe behavior inside of `__post_init__`.
.

from libcst.

bgw avatar bgw commented on August 22, 2024

Hey, @zhammer. Thanks for your interest in LibCST!

Unfortunately we only support 3.7's syntax right now. Sorry for the confusing documentation around python_version. It's there because we intend to support multiple versions of Python syntax in the future. We should probably improve that and raise a ValueError if we see anything other than 3.7.x.

This is actually a limitation in Parso's tokenizer.

CPython 3.6's tokenizer contains a (really ugly) hack that tracks whether you're inside an async function or not, and decides which kind of token to emit for the async and await keywords.

Black's tokenizer (which is similar to Parso's) contains a version of this logic. I think we'd need to port that logic from Black to Parso to fix this problem.

I've upstreamed this issue as davidhalter/parso#83.

Out of curiosity: How many of these syntax errors do you have in your codebase? If it's small enough, it might be worth fixing them by hand, especially seeing as this is a requirement for upgrading to Python 3.7+ anyways.

from libcst.

zhammer avatar zhammer commented on August 22, 2024

Ah, interesting. So, I actually had thought it'd be a fun test-drive of LibCST to automate the 3.6 -> 3.7 syntax refactor - specifically the async keyword case as the codemod seemed pretty straightforward. That being said, it's no problem to do the 3.7 refactor manually.

Thanks for tagging me on those issues! Will follow along.

Also, this was a (very ugly) hack I used to get libcst to run on 3.6 code with async keyword, but at least was able to check my visitor output on those files.

diff --git a/libcst/_parser/_base_parser.py b/libcst/_parser/_base_parser.py
index b143a15..70f2afc 100644
--- a/libcst/_parser/_base_parser.py
+++ b/libcst/_parser/_base_parser.py
@@ -95,6 +95,7 @@ class BaseParser(Generic[_TokenT, _TokenTypeT, _NodeT]):
         self.tokens = tokens
         self.lines = lines
         self._pgen_grammar = pgen_grammar
+        pgen_grammar.__dict__['reserved_syntax_strings'].pop('async', None)
         first_dfa = pgen_grammar.nonterminal_to_dfas[start_nonterminal][0]
         self.stack = [StackNode(first_dfa)]
         self.__was_parse_called = False

Let me know if you'd want a PR for the non-3.7 ValueError.

from libcst.

bgw avatar bgw commented on August 22, 2024

I've submitted #37 to raise a ValueError when we receive a non-3.7 python version. It also should improve the documentation for PartialParserConfig.

@zhammer if it's okay with you, I think I'll close this once #37 lands, since 3.6 support is an upstream issue and it sounds like Parso isn't interested in supporting 3.6's syntax (davidhalter/parso#83).

from libcst.

bgw avatar bgw commented on August 22, 2024

@zhammer, I know we said that we weren't planning on supporting it, but it turns out this may become a reality with #60 and #63.

from libcst.

zhammer avatar zhammer commented on August 22, 2024

awesome, thanks for keeping me in mind for the update :) i've had a lot of fun experimenting with libcst so far!

from libcst.

zhammer avatar zhammer commented on August 22, 2024

I’m traveling but can test once back! An easy way to test, I imagine, would be a one line file: async = True. This is valid 3.6 syntax as async isn’t a reserved keyword.

from libcst.

DragonMinded avatar DragonMinded commented on August 22, 2024

I have tests for that in the tokenizer (even better, the single line async is valid python, its a variable lookup which throws a NameError at runtime). I'm worried more about the fact that the tokenizer hacks had to be ported from lib2to3 to parso and might have some issues on complex edge cases. If you run into any, feel free to open an issue and I'll get it looked at.

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.