Skip to content

[Bug] add_label() incorrectly ignores x=0 and y=0 due to falsy value check #4

@Tony935

Description

@Tony935

Bug Description

In originpro/base.py, the add_label() method uses a falsy value check (x if x else ...) to determine whether the
user has provided x or y coordinates. This causes the method to incorrectly ignore x=0 and y=0, treating them as
if no coordinate was provided and defaulting to the layer center instead.

Steps to Reproduce

  1. Create a graph layer
  2. Call gl.add_label("test", x=0, y=0)
  3. Observe that the label is NOT placed at coordinates (0, 0), but at the center of the layer
import originpro as op

op.set_show()
gp = op.new_graph()
gl = gp[0]
label = gl.add_label("test", x=0, y=0)
  • Expected: label at (0, 0)
  • Actual: label at the center of the layer (e.g., (5.0, 5.0) or similar)

Root Cause

In originpro/base.py, lines 607-608:

x1 = x if x else (self.get_float('x.from') + self.get_float('x.to')) /2
y1 = y if y else (self.get_float('y.from') + self.get_float('y.to')) /2

Since 0 is a falsy value in Python, x if x else ... evaluates to the else branch when x=0, which is incorrect.
The same applies to y=0.

Suggested Fix

Replace the falsy check with an explicit None check:

x1 = x if x is not None else (self.get_float('x.from') + self.get_float('x.to')) / 2
y1 = y if y is not None else (self.get_float('y.from') + self.get_float('y.to')) / 2

This correctly distinguishes between "the user did not provide a value" (None) and "the user provided zero" (0).

Environment

  • OS: Windows 10
  • Origin version: Origin 2023
  • Python version: 3.9.19
  • Originpro version: 1.1.15

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions