Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse Dimensions Function Appears to Be Incorrect #1254

Open
sebastiancyndx opened this issue Mar 10, 2025 · 2 comments
Open

Parse Dimensions Function Appears to Be Incorrect #1254

sebastiancyndx opened this issue Mar 10, 2025 · 2 comments

Comments

@sebastiancyndx
Copy link

sebastiancyndx commented Mar 10, 2025

Describe the bug
The parse_dimension function does not handle '%' and converts to int instead of float.

def parse_dimension(value: str) -> int:
    """Parse dimension value, handling px units"""
    if value.lower().endswith('px'):
        value = value[:-2]  # Remove 'px' suffix
    try:
        return int(value)  # Convert to float first to handle decimal values
    except ValueError as e:
        print(f"Error parsing dimension value {value}: {e}")
        return None

It should be like this:

def parse_dimension(value: str) -> float:
    """Parse dimension value, handling px units, percentages, and decimals."""
    value = value.strip().lower()
    
    if value.endswith('px'):
        value = value[:-2]  # Remove 'px' suffix
    elif value.endswith('%'):
        value = value[:-1]  # Remove '%' suffix
    
    try:
        return float(value)  # Convert to float to handle decimals
    except ValueError as e:
        print(f"Error parsing dimension value '{value}': {e}")
        return None

To Reproduce
Steps to reproduce the behavior:

  1. Go to
    def parse_dimension(value: str) -> int:

Expected behavior
Not fail on dimensions that have a '%' or a decimal

Screenshots

Additional context
Add any other context about the problem here.

@mendeel
Copy link

mendeel commented Mar 11, 2025

Tried your solution and it fixed for me. Thanks!

@ElishaKay
Copy link
Collaborator

ElishaKay commented Mar 16, 2025

Welcome @sebastiancyndx and @mendeel & thanks for the fix

Green light for the PR for whoever would like to carve their name into the git tree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants