pycompatibility


Namepycompatibility JSON
Version 1.0.0 PyPI version JSON
download
home_pageNone
SummaryPython3 library for checking code compatibility with different Python versions.
upload_time2024-07-30 20:11:20
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseMIT license
keywords compatibility python code check
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pycompatibility

![PyPI - Downloads](https://img.shields.io/pypi/dm/pycompatibility)
![PyPI - License](https://img.shields.io/pypi/l/pycompatibility)
![GitHub Tag](https://img.shields.io/github/v/tag/JuanBindez/pycompatibility?include_prereleases)
<a href="https://pypi.org/project/pycompatibility/"><img src="https://img.shields.io/pypi/v/pycompatibility" /></a>

## Python3 library for checking code compatibility with different Python versions.

#### is a tool designed to help verify the compatibility of Python code with versions older than Python 3.8. Specifically, this tool analyzes Python files for features introduced after Python 3.7 and provides refactoring suggestions to ensure that the code can run on older versions.


## Features

#### The pycompatibility detects and reports the use of the following features introduced after Python 3.7:

* `Walrus Operator (:=)`: Introduced in Python 3.8, this operator allows variable assignment within expressions. The tool detects its usage and suggests refactoring to avoid its use if maintaining compatibility with older versions is necessary.

* `Positional-Only Parameters`: Introduced in Python 3.8, these parameters are defined with the / syntax. The tool alerts you to their use and recommends refactoring to ensure compatibility with Python 3.7.

* `Assignment Expressions in Comprehensions`: Introduced in Python 3.9, these expressions allow variable assignment within comprehensions. The tool identifies these expressions and suggests alternatives.

* `Type Union Operator (|)`: Introduced in Python 3.10, this operator allows combining types in annotations. The tool detects the use of this operator and suggests replacing it with Optional from the typing module.

* `Structural Pattern Matching (match-case)`: Introduced in Python 3.10, structural pattern matching allows code to be organized based on patterns. The tool identifies this construct and suggests avoiding its use if compatibility with older versions is required.

* `Enhanced F-strings`: F-strings were enhanced in Python 3.8. The tool detects the use of f-strings and provides refactoring suggestions if needed.


### Install

    pip install pycompatibility

### Command line just pass the name of the script as an argument which will show compatibility

    pycompatibility example_code.py


### In this example you can test a script that contains features that were introduced after version 3.7 of Python, this helps you maintain compatibility with older versions

#### script for testing "example_code.py"


```python
def example_function():
    x = 10
    y = (z := x + 5)  # Walrus operator (3.8+)
    print(f"The value of z is: {z}")  # f-string (3.6+)

    def inner_function(a: int | None):  # Type union operator (3.10+)
        if a is None:
            return "a is None"
        return f"a is {a}"

    result = inner_function(None)
    print(result)

    match result:  # Structural pattern matching (3.10+)
        case "a is None":
            print("Matched None")
        case _:
            print("Matched something else")

    items = [1, 2, 3, 4, 5]
    if any((n := x) > 3 for x in items):  # Walrus operator in comprehensions (3.9+)
        print(f"Found an item greater than 3: {n}")

    context = (open("file1.txt"), open("file2.txt"))  # Parenthesized context managers (3.9+)
    with context as (f1, f2):
        print(f1.read(), f2.read())

    def positional_only_func(a, b, /, c):  # Positional-only parameters (3.8+)
        print(a, b, c)

    positional_only_func(1, 2, 3)
    

```

#### Now just test the script as in the instruction below

```python

>>> from pycompatibility import CompatibilityChecker
>>> 
>>> checker = CompatibilityChecker("example_code.py")
>>> checker.verify()
Line 7: Use of the type union operator '|' detected. Introduced in Python 3.10+.
Suggestion: Replace 'int | None' with 'Optional[int]' from the 'typing' module.

Line 4: Use of the walrus operator ':=' detected. Introduced in Python 3.8+.
Suggestion: Refactor to avoid using the walrus operator ':='.

Line 22: Use of the walrus operator ':=' detected. Introduced in Python 3.8+.
Suggestion: Refactor to avoid using the walrus operator ':='.

Line 29: Use of positional-only parameters detected. Introduced in Python 3.8+.
Suggestion: Consider refactoring parameters if targeting Python 3.7.

Line 5: Use of f-strings detected. Introduced in Python 3.6, but with enhanced features in Python 3.8+.
Suggestion: Consider refactoring f-strings if targeting an older version of Python.

Line 10: Use of f-strings detected. Introduced in Python 3.6, but with enhanced features in Python 3.8+.
Suggestion: Consider refactoring f-strings if targeting an older version of Python.

Line 23: Use of f-strings detected. Introduced in Python 3.6, but with enhanced features in Python 3.8+.
Suggestion: Consider refactoring f-strings if targeting an older version of Python.

Line 15: Use of structural pattern matching (match-case) detected. Introduced in Python 3.10+.
Suggestion: Refactor to avoid using structural pattern matching.

>>> 


```


## Contributing

* If you want to contribute to the project, please submit a pull request or open an issue to discuss changes or improvements.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pycompatibility",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "compatibility, python, code, check",
    "author": null,
    "author_email": "Juan Bindez <juanbindez780@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/4b/02/7c3cd4ccc9e552847359ec4febd597c629fa90e5375a48fa3e151246b00b/pycompatibility-1.0.0.tar.gz",
    "platform": null,
    "description": "# pycompatibility\n\n![PyPI - Downloads](https://img.shields.io/pypi/dm/pycompatibility)\n![PyPI - License](https://img.shields.io/pypi/l/pycompatibility)\n![GitHub Tag](https://img.shields.io/github/v/tag/JuanBindez/pycompatibility?include_prereleases)\n<a href=\"https://pypi.org/project/pycompatibility/\"><img src=\"https://img.shields.io/pypi/v/pycompatibility\" /></a>\n\n## Python3 library for checking code compatibility with different Python versions.\n\n#### is a tool designed to help verify the compatibility of Python code with versions older than Python 3.8. Specifically, this tool analyzes Python files for features introduced after Python 3.7 and provides refactoring suggestions to ensure that the code can run on older versions.\n\n\n## Features\n\n#### The pycompatibility detects and reports the use of the following features introduced after Python 3.7:\n\n* `Walrus Operator (:=)`: Introduced in Python 3.8, this operator allows variable assignment within expressions. The tool detects its usage and suggests refactoring to avoid its use if maintaining compatibility with older versions is necessary.\n\n* `Positional-Only Parameters`: Introduced in Python 3.8, these parameters are defined with the / syntax. The tool alerts you to their use and recommends refactoring to ensure compatibility with Python 3.7.\n\n* `Assignment Expressions in Comprehensions`: Introduced in Python 3.9, these expressions allow variable assignment within comprehensions. The tool identifies these expressions and suggests alternatives.\n\n* `Type Union Operator (|)`: Introduced in Python 3.10, this operator allows combining types in annotations. The tool detects the use of this operator and suggests replacing it with Optional from the typing module.\n\n* `Structural Pattern Matching (match-case)`: Introduced in Python 3.10, structural pattern matching allows code to be organized based on patterns. The tool identifies this construct and suggests avoiding its use if compatibility with older versions is required.\n\n* `Enhanced F-strings`: F-strings were enhanced in Python 3.8. The tool detects the use of f-strings and provides refactoring suggestions if needed.\n\n\n### Install\n\n    pip install pycompatibility\n\n### Command line just pass the name of the script as an argument which will show compatibility\n\n    pycompatibility example_code.py\n\n\n### In this example you can test a script that contains features that were introduced after version 3.7 of Python, this helps you maintain compatibility with older versions\n\n#### script for testing \"example_code.py\"\n\n\n```python\ndef example_function():\n    x = 10\n    y = (z := x + 5)  # Walrus operator (3.8+)\n    print(f\"The value of z is: {z}\")  # f-string (3.6+)\n\n    def inner_function(a: int | None):  # Type union operator (3.10+)\n        if a is None:\n            return \"a is None\"\n        return f\"a is {a}\"\n\n    result = inner_function(None)\n    print(result)\n\n    match result:  # Structural pattern matching (3.10+)\n        case \"a is None\":\n            print(\"Matched None\")\n        case _:\n            print(\"Matched something else\")\n\n    items = [1, 2, 3, 4, 5]\n    if any((n := x) > 3 for x in items):  # Walrus operator in comprehensions (3.9+)\n        print(f\"Found an item greater than 3: {n}\")\n\n    context = (open(\"file1.txt\"), open(\"file2.txt\"))  # Parenthesized context managers (3.9+)\n    with context as (f1, f2):\n        print(f1.read(), f2.read())\n\n    def positional_only_func(a, b, /, c):  # Positional-only parameters (3.8+)\n        print(a, b, c)\n\n    positional_only_func(1, 2, 3)\n    \n\n```\n\n#### Now just test the script as in the instruction below\n\n```python\n\n>>> from pycompatibility import CompatibilityChecker\n>>> \n>>> checker = CompatibilityChecker(\"example_code.py\")\n>>> checker.verify()\nLine 7: Use of the type union operator '|' detected. Introduced in Python 3.10+.\nSuggestion: Replace 'int | None' with 'Optional[int]' from the 'typing' module.\n\nLine 4: Use of the walrus operator ':=' detected. Introduced in Python 3.8+.\nSuggestion: Refactor to avoid using the walrus operator ':='.\n\nLine 22: Use of the walrus operator ':=' detected. Introduced in Python 3.8+.\nSuggestion: Refactor to avoid using the walrus operator ':='.\n\nLine 29: Use of positional-only parameters detected. Introduced in Python 3.8+.\nSuggestion: Consider refactoring parameters if targeting Python 3.7.\n\nLine 5: Use of f-strings detected. Introduced in Python 3.6, but with enhanced features in Python 3.8+.\nSuggestion: Consider refactoring f-strings if targeting an older version of Python.\n\nLine 10: Use of f-strings detected. Introduced in Python 3.6, but with enhanced features in Python 3.8+.\nSuggestion: Consider refactoring f-strings if targeting an older version of Python.\n\nLine 23: Use of f-strings detected. Introduced in Python 3.6, but with enhanced features in Python 3.8+.\nSuggestion: Consider refactoring f-strings if targeting an older version of Python.\n\nLine 15: Use of structural pattern matching (match-case) detected. Introduced in Python 3.10+.\nSuggestion: Refactor to avoid using structural pattern matching.\n\n>>> \n\n\n```\n\n\n## Contributing\n\n* If you want to contribute to the project, please submit a pull request or open an issue to discuss changes or improvements.\n",
    "bugtrack_url": null,
    "license": "MIT license",
    "summary": "Python3 library for checking code compatibility with different Python versions.",
    "version": "1.0.0",
    "project_urls": {
        "Bug Reports": "https://github.com/JuanBindez/pycompatibility/issues",
        "Homepage": "https://github.com/JuanBindez/pycompatibility",
        "Read the Docs": "http://pycompatibility.readthedocs.io/"
    },
    "split_keywords": [
        "compatibility",
        " python",
        " code",
        " check"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2bac74c95851b23ddd4a3e39374041dc53b467b2eb94d2b5b357143e3e83b814",
                "md5": "18058586fc7968037edc537a09a92ebc",
                "sha256": "1240a5cdd033add111a8ac0b94c59f92227fbd6bfedf3863a230f7f1a81cf8b8"
            },
            "downloads": -1,
            "filename": "pycompatibility-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "18058586fc7968037edc537a09a92ebc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 7228,
            "upload_time": "2024-07-30T20:11:18",
            "upload_time_iso_8601": "2024-07-30T20:11:18.613273Z",
            "url": "https://files.pythonhosted.org/packages/2b/ac/74c95851b23ddd4a3e39374041dc53b467b2eb94d2b5b357143e3e83b814/pycompatibility-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4b027c3cd4ccc9e552847359ec4febd597c629fa90e5375a48fa3e151246b00b",
                "md5": "48d379228ed7596edde72ee8acd5510d",
                "sha256": "ad01e27e0a7941e93159af4a502a9852895a3c6ec04f60f7bedc4be5ad59428d"
            },
            "downloads": -1,
            "filename": "pycompatibility-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "48d379228ed7596edde72ee8acd5510d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 7150,
            "upload_time": "2024-07-30T20:11:20",
            "upload_time_iso_8601": "2024-07-30T20:11:20.449906Z",
            "url": "https://files.pythonhosted.org/packages/4b/02/7c3cd4ccc9e552847359ec4febd597c629fa90e5375a48fa3e151246b00b/pycompatibility-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-30 20:11:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "JuanBindez",
    "github_project": "pycompatibility",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pycompatibility"
}
        
Elapsed time: 0.36790s