# Understanding `python.analysis.useLibraryCodeForTypes` in Pylance

[Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) is a fast, feature-rich language server extension for Python in Visual Studio Code, powered by the Pyright type checker. It provides advanced type checking, auto-completions, code navigation, and other language features to enhance your Python development experience.

One important Pylance setting is `python.analysis.useLibraryCodeForTypes`, which affects how Pylance handles type information from third-party libraries that do not provide type stubs (such as `.pyi` files) or inline type annotations.

This guide explains what `python.analysis.useLibraryCodeForTypes` does, how it impacts your development experience, and how you can configure it to suit your needs.

## What is `python.analysis.useLibraryCodeForTypes`?

In Python, third-party libraries can provide type information to type checkers like Pylance in two ways:

1. **Type Stubs**: Separate `.pyi` files that describe the types of functions, classes, and variables in the library.

2. **Inline Type Annotations**: Type hints included directly in the library's source code (Python 3.5 and later).

However, not all libraries provide type stubs or inline type annotations. In such cases, Pylance can attempt to infer type information by parsing the library's source code. This is where the `python.analysis.useLibraryCodeForTypes` setting comes into play.

If you want to provide better type information instead of falling back to library-source inference, see [`python.analysis.stubPath`](python_analysis_stubPath.md) for package-specific custom stubs and [`python.analysis.typeshedPaths`](python_analysis_typeshedPaths.md) for a custom typeshed tree.

### Purpose of the Setting

- **`true`**: Pylance will parse the library's source code to infer type information when type stubs are not available.
- **`false`**: Pylance will not parse the library's source code and treats all types from the library as `Any`.

By default, Pylance sets `python.analysis.useLibraryCodeForTypes` to `true`.

## How Does It Affect Your Development Experience?

### When Enabled (`true`)

- **Enhanced IntelliSense**: Pylance provides better auto-completion suggestions, hover information, and code navigation for third-party libraries without type stubs.
- **Type Checking Errors**: Pylance may report type checking errors or warnings based on its inferred types, which could sometimes be incorrect due to limitations in static analysis.
- **Performance Impact**: Parsing and analyzing library source code can increase Pylance's resource usage, potentially affecting performance, especially with large libraries.

### When Disabled (`false`)

- **Reduced IntelliSense**: Pylance treats all types from untyped libraries as `Any`, resulting in less informative auto-completion and hover information.
- **Fewer False Positives**: Since types are treated as `Any`, Pylance will not report type checking errors originating from untyped libraries, reducing potential false positives.
- **Performance Improvement**: Without the need to parse library source code, Pylance may perform better, especially in large projects or when working with heavy libraries.

## How to Change the Setting

To adjust the `python.analysis.useLibraryCodeForTypes` setting in Visual Studio Code:

1. **Open the Settings**:
    - Click on the gear icon in the lower-left corner and select **Settings**.
2. **Search for the Setting**:
    - In the search bar at the top, type `useLibraryCodeForTypes`.
3. **Modify the Setting**:
    - Find the **Python › Analysis: Use Library Code For Types** setting.
    - Uncheck the box to set it to `false` (disable parsing of library code), or check it to set it to `true`.

Alternatively, you can edit your `settings.json` file directly:

1. **Open Command Palette**:
    - Click on the gear icon in the lower-left corner and select **Command Palette**.
2. **Open Settings (JSON)**:
    - Type `Preferences: Open Settings (JSON)` and select it.
3. **Add or Modify the Setting**:
    ```json
    "python.analysis.useLibraryCodeForTypes": false
    ```

## When and Why to Disable `useLibraryCodeForTypes`

### Dealing with Unannotated Third-Party Libraries

Many third-party libraries do not provide type stubs or inline type annotations. Pylance attempts to infer types by parsing their source code, but this process can sometimes lead to incorrect type inferences, resulting in false positives during type checking.

Disabling `useLibraryCodeForTypes` instructs Pylance to treat all types from these libraries as `Any`, effectively disabling type checking for them. This can reduce clutter from unnecessary warnings or errors related to type checking of library code.

### Improving Performance in Large Projects

Parsing and analyzing the source code of large libraries can consume significant resources and slow down Pylance's responsiveness. Disabling this setting can improve performance by reducing the amount of code Pylance needs to analyze.

### Examples of When to Disable

- **Working with Unannotated Libraries**: If you are using a library that doesn't offer type stubs and Pylance is generating many false positives or irrelevant type checking errors from that library.
- **Encountering Performance Issues**: If you notice that Pylance is consuming high CPU usage or is slow to respond, especially when working with large libraries or codebases.
- **Focusing on Your Code**: You prefer Pylance to focus on type checking your own code and not the code from third-party libraries.

Before disabling this setting globally, consider whether adding custom stubs through [`python.analysis.stubPath`](python_analysis_stubPath.md) would solve the problem more precisely.

### Example Scenario

Suppose you're using a library `examplelib` that doesn't have type stubs or type annotations. Pylance, with `useLibraryCodeForTypes` enabled, tries to infer types from `examplelib`, but this leads to incorrect type errors in your code:

```python
import examplelib

def process_data(data):
    examplelib.process(data)  # Pylance reports type errors here
```

By setting `python.analysis.useLibraryCodeForTypes` to `false`, Pylance will treat `examplelib` functions as returning `Any`, and will not report type errors originating from its inferred types.

## Examples

### Before Disabling `useLibraryCodeForTypes`

```python
import unknownlib

result = unknownlib.calculate(5, 10)  # Pylance infers types and may report errors
print(result)
```

Pylance may report type errors based on its inference from `unknownlib`, such as:

- "Argument of type 'int' cannot be assigned to parameter 'data' of type 'str' in function 'calculate'"
- "Unknown member 'calculate' for module 'unknownlib'"

### After Disabling `useLibraryCodeForTypes`

```python
import unknownlib

result = unknownlib.calculate(5, 10)  # Pylance treats 'calculate' as returning 'Any'
print(result)
```

Pylance no longer reports type errors related to `unknownlib`, and your code remains clean from false positives.

## Frequently Asked Questions

### Q: What exactly does `python.analysis.useLibraryCodeForTypes` do?

**A:** It controls whether Pylance parses the source code of third-party libraries to infer type information when type stubs are not available. When set to `true`, Pylance attempts to infer types by parsing library code. When set to `false`, Pylance treats types from such libraries as `Any`.

### Q: Will disabling `useLibraryCodeForTypes` affect code completion for libraries?

**A:** Yes, it can affect code completion and other IntelliSense features for libraries without type stubs or inline type annotations. Pylance will have less information about the types and members of such libraries, leading to reduced auto-completion suggestions.

### Q: If I disable `useLibraryCodeForTypes`, can I still get type checking for some libraries?

**A:** Yes, libraries that provide type stubs (`.pyi` files) or inline type annotations will still offer type information to Pylance. Disabling this setting only affects libraries without such type information.

### Q: How do I know if a library provides type stubs or type annotations?

**A:** You can check the library's documentation or look for a `py.typed` file in its package, which indicates that it includes type hints. Libraries that have type stubs often list this information in their installation or usage instructions.

### Q: Can I disable type checking for specific libraries only?

**A:** The `python.analysis.useLibraryCodeForTypes` setting is global and cannot be used to disable type checking for specific libraries. To manage type checking for particular libraries, you can create stub files for the library through [`python.analysis.stubPath`](python_analysis_stubPath.md), point Pylance at a custom typeshed tree through [`python.analysis.typeshedPaths`](python_analysis_typeshedPaths.md) when the problem is typeshed-based, or use `# type: ignore` comments in your code.

## Related Diagnostics

When `useLibraryCodeForTypes` is `false`, untyped libraries return `Unknown` types, which can trigger strict-mode rules:

- [`reportUnknownVariableType`](../diagnostics/reportUnknownVariableType.md) — variable is `Unknown`
- [`reportUnknownArgumentType`](../diagnostics/reportUnknownArgumentType.md) — argument is `Unknown`
- [`reportUnknownMemberType`](../diagnostics/reportUnknownMemberType.md) — attribute returns `Unknown`
- [`reportUnknownParameterType`](../diagnostics/reportUnknownParameterType.md) — parameter is `Unknown`
- [`reportMissingTypeStubs`](../diagnostics/reportMissingTypeStubs.md) — no type stubs found for a library

If these rules are noisy for specific libraries, consider adding stubs via [`stubPath`](python_analysis_stubPath.md) or installing type stubs (`pip install types-<package>`).

## See Also

- [How to Fix Unresolved Import Errors](../howto/unresolved-imports.md) — when library code analysis affects import resolution
- [How to Tune Pylance Performance](../howto/performance-tuning.md) — performance impact of analyzing library source
- [How to Troubleshoot Settings](../howto/settings-troubleshooting.md) — what happens when `pyrightconfig.json` overrides this setting

---

_For more detailed information on Pylance settings and customization, refer to the **[Pylance Settings and Customization](https://code.visualstudio.com/docs/python/settings-reference)** documentation._

---

_"This document was generated with the assistance of AI and has been reviewed by humans for accuracy and completeness."_
