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.
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.
* 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
* 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
* 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
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`.
* 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>
* 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,...
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.
* 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
* 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
* Support module and package names in the codemod context
* PR feedback
* Reorganize module name and relative name logic to libcst.helpers.module
* Force rebuild
* Port c3b44cb9d3
* Port 138c97cb70
* Test harness for the next commit
* Port 2cdc4ba237
* Test harness for next commit
* Port 71c5da8169
* Remove no-longer-used import
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>
* 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
* 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
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