Skip to content

Issue with using AttackArgs class outside of the command line (in my Python script) #728

Closed

Description

@sabrinahartung

Hello,

I am experiencing an issue when trying to use the AttackArgs class from the TextAttack library outside of the command line. I'm attempting to use the class programmatically in my Python script, but the arguments I provide are not correctly assigned.

Here's a code snippet demonstrating my problem:

from textattack.attack_args import AttackArgs

attack_args = AttackArgs(
num_examples=100, 
num_successful_examples=2, 
log_to_csv="text_attack_results.csv"
)

print(attack_args)

When I run the code, the num_examples values are not updated and remain at their default values. However, it seems that other arguments like num_successful_examples or log_to_csv are being correctly assigned.

I suspect this might be related to the class implementation, specifically the _add_parser_args method, which seems to be designed for command-line usage. Is there a way to use the AttackArgs class programmatically in a Python script without relying on the command line interface? Any guidance or suggestions would be appreciated.

Thank you for your assistance.

System Information:

  • OS: MacOS Ventura 13.1
  • Textattack version: 0.3.8

Activity

  1. sabrinahartung commented on Apr 14, 2023

    @sabrinahartung
    Author

    I discovered that the issue with num_examples being set to None is due to the following code snippet in the AttackArgs class:

    def post_init(self):
    if self.num_successful_examples:
    self.num_examples = None

    This is causing num_examples to be set to None when a value is assigned to num_successful_examples.

    I would like to understand if this behavior is intentional and, if so, the rationale behind it. Is it to prevent users from specifying both num_examples and num_successful_examples simultaneously? If this is a design choice, could you please provide guidance on how to use these options properly in cases where we want to limit the total number of examples while also specifying the number of successful adversarial examples desired? If this is a bug, can you suggest a workaround or provide an update to fix the issue?

  2. qiyanjun commented on Aug 13, 2026

    @qiyanjun
    Member

    @sabrinahartung you diagnosed this exactly right — it's intentional (per the docstring: "If set, this argument overrides `num_examples`"), but silently dropping the value you passed with no explanation is a bad experience. Opened #831, which adds a warning when both are set together, so this is at least discoverable without reading the source. Will close automatically once that merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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