Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an ignore mechanism #21

Open
Lawouach opened this issue Jan 25, 2017 · 9 comments
Open

Provide an ignore mechanism #21

Lawouach opened this issue Jan 25, 2017 · 9 comments

Comments

@Lawouach
Copy link

It may happen that your Python code contains statements that are necessary for that Python code to run in Python but aren't required in JS.

Or, for instance, you maybe interested in hinting types in Python using the typing module but you don't want to import that module in JS.

Right now, this:

from typing import List

gets transformed to (es5):

var _typing = require("typing");

The hints are only for the Python code benefits and should be ignored while transpiling.

Could we have come up with a solution so that simple statements are left out?

@Lawouach
Copy link
Author

I'm wondering if a comment-style approach would be simpler here?

from typing import List  # pj:notranspile

?

@azazel75
Copy link
Collaborator

It would be indeed much more simpler, unfortunately the standard Python's AST parser trashes all the comments. I want to use the typed_ast module to do type inference and it support type annotations in comments, maybe it can be also used for this purpose.

@Lawouach
Copy link
Author

Oh really? Damn stupid AST parser :(

There is also this module http://redbaron.readthedocs.io/en/latest/

@Lawouach
Copy link
Author

But I like the idea of the typed_ast module!

@azazel75
Copy link
Collaborator

It seems that typed_ast only allows comments on some lines, like def statements... and never on import statements.. @Lawouach that coverage library that you mentioned uses redbaron?

@Lawouach
Copy link
Author

Nope, the ast module but I think they do their own trick

https://github.com/nedbat/coveragepy/blob/master/coverage/parser.py

Search for the self.exclude variable.

@lelit
Copy link
Contributor

lelit commented Apr 29, 2017

https://github.com/PyCQA/baron is effectively interesting, but it would require quite a lot of work to adapt PJ to that, even if it supported Python 3.

@icarito
Copy link
Contributor

icarito commented Jan 25, 2018

I have needed this when using other python transpilers - Transcrypt uses a function (I think JS), and Rapydscript a special kind of string (v-string: v'console.log("hello from js")').

@jayvdb
Copy link

jayvdb commented May 29, 2021

I am using strip-hints then autoflake to remove the hints.

py2many uses a very crude but effective import remover to strip out imports which wont have any meaning in the target language.

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

No branches or pull requests

5 participants