Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Modernize dynamic imports: Replace exec/eval with importlib and use stdlib cached_property #1386

@samay2504

Description

@samay2504

Summary

The [pysal/base.py] and [pysal/lib/common.py] modules use outdated Python patterns that should be modernized for better security, maintainability, and to follow current best practices.

Problems Identified

1. Custom cached_property implementation (pysal/base.py, lines 23-39)

PySAL defines its own cached_property class, but since PySAL requires Python >= 3.10 (per pyproject.toml), the standard library's functools.cached_property (available since Python 3.8) should be used instead.

2. Unsafe use of exec() and eval() (pysal/base.py, pysal/lib/common.py)

The codebase uses exec() and eval() for dynamic module imports:

# In base.py
def _installed_version(package):
    try:
        exec(f'import {package}')
    except ModuleNotFoundError:
        v = 'NA'
    try:
        v = eval(f'{package}.__version__')
    except AttributeError:
        v = 'NA'
    return v

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions