The cat doesn't want to be petted.
If your cat claws you for nothing they most likely have too much energy and need a scratching post. Or sometimes your cat is trying to tell you something like, "I don't want to be petted right now" or,"I'm hungry and have no food".
A cat's claws are retractable due to an elastic ligament and tendon in their paw that allows the claws to extend and retract as needed. When a cat is resting or walking, their claws are retracted to prevent them from getting damaged or becoming blunt. The ability to retract their claws also helps cats maintain stealth and agility while hunting or climbing.
The cat may not want to be petted.
A cat may show its teeth when petted as a warning sign that it is feeling uncomfortable or overstimulated. This behavior is a way for the cat to communicate that it wants the petting to stop.
Cats have individual preferences, so it's possible that your cat enjoys being petted but not being held. Watch your cat's body language for clues on what they prefer.
Cats extend their claws to help them climb, scratch, defend themselves, and mark their territory.
A cat can retract it's claws.
He liked how you petted him.
Yes, a cat has dug its claws into me before.
Raccoon claws are not retractable.
When a cat shows its claws they are unsheathed. A cat may unsheathe its claws in an attempt to protect itself from a perceived danger or in play.