gritql icon indicating copy to clipboard operation
gritql copied to clipboard

Allow for non-standard suffixes/filenames for languages

Open dshahbaz opened this issue 1 year ago • 1 comments

I have files that should be treated as python, but do not end in .py. I can't convince grit to respect the files and treat them as python with any option or config value that I've found. Instead, grit fails with:

ERROR (code: 410) - Skipped <mypath here> since it is not a python file

My workaround is hardlinking to .py, but it would be ideal to force grit to ignore the suffix, or to register alternative patterns as a file type using grit.yaml.

dshahbaz avatar Sep 04 '24 15:09 dshahbaz

I think there's two immediate things we could do:

  • Adjust this logic so that if the path of the file is directly specified (vs. being discovered via traversal) we always attempt to parse it.
  • Add an --extensions flag to override which extensions are parsed at runtime.

Happy to review a PR.

morgante avatar Sep 04 '24 20:09 morgante

I think option #1 is best.

morgante avatar Oct 24 '24 00:10 morgante

/bounty $10

morgante avatar Oct 24 '24 00:10 morgante

💎 $10 bounty • Grit

Steps to solve:

  1. Start working: Comment /attempt #485 with your implementation plan
  2. Submit work: Create a pull request including /claim #485 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to getgrit/gritql!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @mobley-trent Oct 24, 2024, 11:14:16 AM WIP
🟢 @Alex-ley-scrub Oct 24, 2024, 10:46:04 PM #562

algora-pbc[bot] avatar Oct 24 '24 00:10 algora-pbc[bot]

/attempt #485

Algora profile Completed bounties Tech Active attempts Options
@mobley-trent 10 bounties from 5 projects
Python, Rust,
Jupyter Notebook
Cancel attempt

mobley-trent avatar Oct 24 '24 11:10 mobley-trent

/attempt #485

@dshahbaz what is the suffix of the file out of curiosity if you don't mind sharing?

It's not "pyi" by any chance is it? Or another (semi) valid/standard python extension? .pyi - Stub file (PEP 484)

I only ask because if it is .pyi I believe they are a true subset of .py files (they're just interface files with function signatues and type hints but no implementations, instead just have ... in the function bodies etc.) and should be able to be parsed just like a normal .py file.

@morgante should we consider to add "pyi" here (I've done that in this commit 63f113c in case the answer is yes, otherwise can revert):

https://github.com/getgrit/gritql/blob/a1b312bdefed4982f7a96304b88577762f7edcf1/crates/language/src/target_language.rs#L290

https://github.com/getgrit/gritql/blob/a1b312bdefed4982f7a96304b88577762f7edcf1/crates/language/src/target_language.rs#L346

This is my minimal reproducible example (added as a test case in this commit 9abf798):

These both fail for me using the released version of grit (with or without --force):

echo 'class MyClass(object): ...' > dummy.nopy
grit apply '`object` => ``' dummy.nopy --lang=python --force
# out: dummy.nopy: ERROR (code: 410) - Skipped dummy.nopy since it is not a python file
# out: Processed 2 files and found 0 matches

and

echo 'class MyClass(object): ...' > dummy.pyi
grit apply '`object` => ``' dummy.pyi --lang=python --force
# out: dummy.pyi: ERROR (code: 410) - Skipped dummy.pyi since it is not a python file
# out: Processed 2 files and found 0 matches

(as an aside the count of processed files seems to be doubled for the failed ones, which I also fixed in this commit ff3fafc)

see my PR attempt coming shortly

Algora profile Completed bounties Tech Active attempts Options
@Alex-ley-scrub 2 grit bounties
Rust
Cancel attempt

Alex-ley-scrub avatar Oct 24 '24 22:10 Alex-ley-scrub

Adding .pyi seems reasonable, as well as fixing the overall problem.

morgante avatar Oct 24 '24 22:10 morgante

/assign @Alex-ley-scrub

morgante avatar Oct 24 '24 22:10 morgante

💡 @Alex-ley-scrub submitted a pull request that claims the bounty. You can visit your bounty board to reward.

algora-pbc[bot] avatar Oct 24 '24 22:10 algora-pbc[bot]

🎉🎈 @Alex-ley-scrub has been awarded $10! 🎈🎊

algora-pbc[bot] avatar Oct 26 '24 20:10 algora-pbc[bot]

Thank you! I haven't tried this yet.

In my use case I was attempting to use bazel BUILD files, which approximately python, at least in terms of syntax.

dshahbaz avatar Oct 27 '24 00:10 dshahbaz