Commit graph

664 commits

Author SHA1 Message Date
Amethyst Reese
f9536b522f
Pass root path to FullyQualifiedNameProvider (#867)
This allows FullyQualifiedNameProvider to work with absolute paths,
rather than assuming all paths given will be relative to the current
directory. This enables tools like Fixit to provide a root path, and
have the FullyQualifiedNameProvider correctly scope the final results
relative to that root path.

This does require that both the root path and the given file paths
match the other as relative or absolute, due to the
`calculate_module_and_package` helper comparing file paths relative
to the root path, but this seems like a reasonable tradeoff, and
unlikely to cause a problem in normal use cases.
2023-02-22 14:36:10 -08:00
Amethyst Reese
d94687e378
Script to regenerate test fixtures (#872)
Upgrading Pyre requires updating test fixtures with any upstream changes
to Pyre's query results for the `simple_class.py` fixture.

This adds a new `scripts/` directory to the repo, with a script to
regenerate test fixtures. The script regenerates the cache data fixture,
and updates the `TypeInferenceProvider` tests to use `assertDictEqual`
and helpful error messages for better behavior in future mismatches.

This also includes a slight bump to Pyre 0.9.10 to fix install issues on
Apple Silicon M1 Macs, and regenerated fixtures using the script above.
2023-02-22 12:35:23 -08:00
Sagar Badiyani
de28541fa3
[AddImportsVisitor] Docstring Check Only for the Top Element of the Body (#841)
* Initial Commit

* lint fix
2023-01-24 13:51:56 +00:00
Carl Meyer
f668e88dd2
fix PEP 604 union annotations in decorators (#828) 2022-11-29 15:24:24 -07:00
Matthew Shaer
c44b182e88
Adding a provider which can tell what accessor to use to go from the parent to that child node (#807) 2022-11-10 09:38:32 +00:00
MapleCCC
973895a6c0
Several trivial refactors (#770)
* Enumeration members are singletons. Copying on them would be no-op

* Avoid generating unnecessary `pass` statement

* Several trivial refactor

* Avoid building unnecessary intermediate lists, which are mere slight waste of time and space

* Remove unused import, an overlook from commit 8e6bf9e9

* `collections.abc.Mapping.get()` defaults to return `None` when key doesn't exist

* Just use unittest's `assertRaises` to specify expected exception types, instead of catching every possible `Exception`s, which could suppress legitimate errors and hide bugs

* We know for sure that the body of `CSTTypedTransformerFunctions` won't be empty, so don't bother with complex formal completeness
2022-09-14 14:33:45 +01:00
MapleCCC
667c713b38
Fix the bug that the use of formatter in codemods has undetermined target Python version, resulting in hard-to-reason-with behavior (#771)
* When codemod, specify the black formatter to use the same target Python version we use

* Fix the `test_codemod_formatter_error_input` unit test

* Remove an unused import in `test_codemod_cli` module
2022-09-14 14:31:36 +01:00
MapleCCC
27aa23f056
Raise informative exception when metadata is unresolved in a metadata-based match (#757)
* Raise informative exception when metadata is unresolved in a metadata-based match, instead of silently hide potential errors

* Fix unit test of `findall`

* Add unit test to cover the case of a resolved metadata provider doesn't provide metadata for all nodes

* Document the behavior of metadata-based match when the metadata provider is unresolved
2022-08-29 16:47:02 +01:00
MapleCCC
64811b7795
Tighten the metadata type of ExpressionContextProvider (#760) 2022-08-29 16:46:08 +01:00
MapleCCC
fc622ce790
Fix bug when TypeOf is one of options in OneOf / AllOf (#756)
* Fix a bug when one of the option of `OneOf` is a `TypeOf`

* Disallow `TypeOf` in `AllOf`, analogous to how `OneOf` is disallowed in `AllOf`
2022-08-26 18:54:07 +01:00
MapleCCC
73cfc7f7fa
Fix docstring of FullRepoManager (#750)
The render error originates from how we violate the syntax rules of the `field list` markup element of reStructuredText. The `specification of field list states](https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#field-lists) that a multi-line `field body` must be indented relative to the `field marker`.
2022-08-17 13:42:31 +01:00
zzl
47e5ea15e1
Fix parse error message for number parsing (#724)
Co-authored-by: zzl0 <zhuzhaolong0@mail.com>
2022-08-04 11:33:26 +01:00
Chenguang Zhu
7cb229d175
Implement lazy loading mechanism for QualifiedNameProvider (#720)
* Implement lazy loading mechanism for expensive metadata providers
* Add support for lazy values in metadata matchers
* Fix type issues and implement lazy value support in base metadata provider too
* Add unit tests for BaseMetadataProvider

Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
2022-07-09 08:34:29 +01:00
Luke Petre
b3eda508d4
Fixing prefix matching bug from 0.4.6 (#719) 2022-07-05 11:25:39 +01:00
Zsolt Dollenstein
343f56f607
[parser] bail on deeply nested expressions (#718) 2022-07-04 14:45:42 +01:00
Zsolt Dollenstein
9925117391
Support whitespace after ParamSlash (#713)
* add whitespace_after field to ParamSlash
* codegen
2022-06-26 09:42:37 +01:00
Luke Petre
aa4a2790db
Remove unnecessary qname work (#709) 2022-06-21 21:35:05 +01:00
Luke Petre
779163701c
Faster qualified name format (#710) 2022-06-21 14:26:45 +01:00
Luke Petre
42164f8672
Cache the scope name prefix to prevent scope traversal in a tight loop (#708)
* Cache the scope name prefix to prevent scope traversal in a tight loop

* Adding pyre-fixme. this attribute iclearly has a type in the base class.

* Clarify why we do join(filter(None,...
2022-06-21 10:11:02 +01:00
Sergei Lebedev
306a5f8175
convert_type_comments now preserves comments following type comments (#702)
For example,

    y = 5  # type: int  # foo

is converted to

    y: int = 5  # foo
2022-06-20 07:39:12 -06:00
zzl
7ca1bd1cd5
expression: fix SimpleString's quote method (#704)
* expression: fix SimpleString's quote method

* Add missing copyright header

Co-authored-by: zzl0 <zhuzhaolong0@mail.com>
Co-authored-by: Luke Petre <lpetre@fb.com>
2022-06-17 13:05:05 +01:00
Luke Petre
6f28c799bb
Fix slow perf in 0.4.2+ (#698) 2022-06-16 12:45:02 +01:00
Zsolt Dollenstein
ebe1851c2b
Add support for PEP-646 (#696) 2022-06-13 09:52:31 -06:00
Luke Petre
149599ee88
Restore the 0.4.1 behavior for libcst.helpers.get_absolute_module (#684) 2022-05-11 07:42:37 -04:00
Luke Petre
c6559671aa
Consider access information when computing qualified names for nodes (#682)
* Write a test case showing qualified names for shadowed assignments

* Consider accesses when looking up names of nodes

* Fix format

* Fix typecheck
2022-05-04 09:50:26 -04:00
Zsolt Dollenstein
c30bbcfa48
make sure ParserError's raw_line is zero-indexed (#681) 2022-05-04 05:51:49 -06:00
toofar
6e0c63ae9c
rename codemod: Correct last renamed import from (#675)
Correct the renamed import from structure when renaming last imported name from a module.

Given

    from a.b import qux

    print(qux)

And providing old_name="a.b.qux" and new_name="a:b.qux" I expect the
following output (as described int the command description):

    from a import b

    print(b.qux)

But what I get is:

    from a import b.qux

    print(b.qux)

It pulls the old name up into the new one.

The provided test is the important part but I've attempted a fix too. I
suspect there is a better one and that the special casing of the "this
is that last name" situation shouldn't be needed. For instance there is
import removing code in leave_Module and renaming the first of many
names (as opposed to the last) happily adds a correct import line.
I didn't manage to grok the code and all the concepts it requires to
provide a better fix though.

This leaves the alias adjustments to the existing code and just does the
module renaming the int he special casing block.
I don't know why scheduling removal of the updated node is required, it makes
the tests pass though.
2022-04-14 17:13:40 -07:00
Martin DeMello
abc566cd4a
add an always_qualify_annotations argument to the type annotation visitor (#676) 2022-04-13 14:23:21 -07:00
Martin DeMello
e6f208c7db
Qualify imported symbols when the dequalified form would cause a conflict (#674)
* Qualify imported symbols when the dequalified form would cause a conflict.

Adds a preliminary pass that scans the stub file for all imported
symbols, and collects the ones that cannot be safely dequalified.

Fixes #673

* review fixes

* handle symbol conflicts between the stub and the main file

* fix type errors
2022-04-12 12:12:55 -07:00
wiyr
0a8ae91d39
fix qualified name get bug (#669)
* fix qualified name get bug

* added unittest
2022-04-08 08:25:15 -07:00
Martin DeMello
869af036e0
Preserve as-imports when merging type annotations. (#664)
* Preserve as-imports when merging type annotations.

Fixes #661

* fix test and bad rebase

* ufmt
2022-04-07 15:12:30 -07:00
Steven Troxler
954bd99d8a
Bump black to latest to address failures in Python 3.9/3.10 (#672)
* Bump black to latest to address failures in Python 3.9/3.10

* Rerun ufmt to adapt to new black version
2022-04-07 11:33:18 -07:00
Steven Troxler
f027b84366
Always use ... in stubs, not pass (#670)
Confirmed that the CI failures are happening against trunk.

I'm attempting to fix by upgrading black

Merging since this is a trivial change
2022-04-07 09:49:33 -07:00
Luke Petre
cf16eccea4
Update relative import logic to match cpython (#660)
* Always compute a module and package name

* Update name_provider to correctly support __main__ (also updated the tests to use data_provider)

* Update name_provider to correctly handle relative imports and package name

* Update relative module resolution to work on package names

* Use full_package_name in libcst.codemod.visitors.GatherImportsVisitor

* Use full_package_name in libcst.codemod.visitors.RemovedNodeVisitor

* Use full_package_name in libcst.codemod.visitors.AddImportsVisitor

* Fix failing test

* Fix typo in variable name

* PR feedback

* Force rebuild
2022-03-24 17:21:08 -04:00
Luke Petre
e5ab7b90b4
Drop support for running libcst using a python 3.6 interpreter (#663)
* Drop support for running libcst using a python 3.6 interpreter

* PR feedback
2022-03-24 11:31:48 -04:00
Luke Petre
914b18339d
Support module and package names in the codemod context (#662)
* Support module and package names in the codemod context

* PR feedback

* Reorganize module name and relative name logic to libcst.helpers.module

* Force rebuild
2022-03-23 14:17:25 -04:00
Shannon Zhu
f863febc4d
Define gather global names visitor (#657)
* Define gather global names visitor

* Quote forward references when applying annotations
2022-03-09 10:53:44 -08:00
Steven Troxler
f7417febe7
Tweak the license format again. (#653) 2022-02-18 12:10:44 -08:00
Steven Troxler
775beec38f
Remove trailing comment line from LICENSE header (#652) 2022-02-18 10:52:08 -08:00
Steven Troxler
e8c84572e4
Port pyre fixes (#651)
* Port c3b44cb9d3

* Port 138c97cb70

* Test harness for the next commit

* Port 2cdc4ba237

* Test harness for next commit

* Port 71c5da8169

* Remove no-longer-used import
2022-02-18 09:56:54 -08:00
Stanislav Levin
bb6d150acd
tests: Fix assumption about sorting in test_ordering (#650)
The test wrongly assumed that `first_assignment.references`
is ordered collection, while actually it is `set`.

Fixes: https://github.com/Instagram/LibCST/issues/442
Signed-off-by: Stanislav Levin <slev@altlinux.org>
2022-02-18 09:10:56 -08:00
Steven Troxler
f2cd39c2b9
Fix the copyright heading on add_trailing_commas.py (#649) 2022-02-16 10:17:28 -08:00
Steven Troxler
0eb839d4f9
Add a DESCRIPTION to ApplyTypeComments (#647) 2022-02-15 11:33:45 -08:00
Steven Troxler
a2e9c4a276
Clean up ApplyTypeAnnotationsVisitor (#646)
* Remove unneeded  block

* Improve function name, add docstring

* Rename _is_set -> _is_non_sentinel

* Add docstring for FunctionKey

* Add class attributes with doc blocks to TypeCollector

* Extract Annotations into a single abstraction, not two

* Nits + fix flake8
2022-02-14 10:10:44 -08:00
Steven Troxler
f018d9924b
Create an AddTrailingCommas codemod (#643)
* Add ApplyTrailingCommas codemod

This codemod adds trailing commas to parameter and arguments
lists when there are sufficient arguments or parameters.

The idea is this:
- both black and yapf will generally split lines when there
  are trailing commas at the end of a parameter / arguments list
- It's easier on my eye to have names and types in more predictable
  locations within a function header, i.e. left-aligned. And in
  function calls, I also find it easier to compare arguments to
  function parameters whenever the arguments are one-per line, at
  least when there are more than two arguments.

By default, we ensure trailing commas for functions with one or more
parameters (but do not include `self` or `cls` method arguments) which
is suitable for `black`, and calls with 3 or more arguments.

Both the parameter count and the argument count can be overridden.
Moreover, by passing `--formatter yapf` someone can use the
yapf-suitable default of 2 parameters which is handy since then the
user doesn't have to memorize black vs yapf settings; this is necesary
because yapf does not split lines after a trailing comma in one-argument
defines.

```
> python -m unittest libcst.codemod.commands.tests.test_add_trailing_commas
......
----------------------------------------------------------------------
Ran 6 tests in 0.134s

OK
```

* Run ufmt, fix type error

* Bump argument counts down to 2
2022-02-11 06:26:33 -08:00
Steven Troxler
0f42a7824b
Apply Type Comments: Allow for skipping quotes when applying type comments (#644)
* Allow for skipping quotes when applying type comments

* Fix bad flag (tests don't check argparse, I ran it on pytorch)

* Run ufmt
2022-02-10 13:43:30 -08:00
Zsolt Dollenstein
3af6820ca7
Fix space validation for AsName and Await (#641)
* Fix space validation for AsName and Await

* Update libcst/_nodes/tests/test_import.py

Co-authored-by: Steven Troxler <steven.troxler@gmail.com>
2022-02-10 10:21:54 -08:00
Steven Troxler
1aa40f799b
Autoformat ApplyTypeAnnotationsVisitor (#642)
I find it difficult sometimes to read method and function signatures
when there are multiple arguments with type annotations - left-aligning
the arguments makes it much easier for me to skim and see, using mostly
my automatic visual resoning,
- the argument names
- the argument types
- the return type

Without this, I feel like I'm trying to run a parser in my head, which
is not as fast and distracts me from code-skimming.

This change was generated using thte new AddTrailingCommas codemod
(which I'll put in a separate PR) via the command
```
python -m libcst.tool codemod add_trailing_commas.AddTrailingCommas ./libcst/codemod/visitors/_apply_type_annotations.py
```

Wait for CI - this is pure formatting, it should be very safe
2022-02-09 17:06:12 -08:00
Zsolt Dollenstein
fb56fa6b8f
[native] Make IntoPy conversion fallible (#639)
* Make IntoPy fallible
* Simplify test case so it works on 3.6
2022-02-07 11:52:29 +00:00
Zsolt Dollenstein
c91655fbba
fix copyright headers and add a script to check (#635) 2022-02-01 11:13:17 +00:00